Objective
A free open-access digital R reference book catered to epidemiologists and public health practitioners that is usable offline and addresses common epidemiological tasks via clear text explanations, step-by-step instructions, and best practice R code examples
Epis using R must often Google search and read dozens of forum pages to complete common data manipulation and visualization epi tasks. However, field epidemiologists often work in low internet-connectivity environments and have limited technical support. This handbook aims to fill this gap.
How to read this handbook:
The is an HTML file which can be viewed offline, and is best viewed with Google Chrome.
Click the “clipboard” icon in the upper-right of each code chunk to copy it.
Package names are written in bold (e.g. dplyr) and functions are written like this: mutate(). Packages referenced either in text or within code like this: dplyr::mutate()
Version
The latest version of this handbook can be found at this github repository.
Emphasis on readability and instruction
We chose to frequently write code on new lines, in order to offer more understandable comments. As a result, code that could be written like this:
obs %>%
group_by(name) %>% # group the rows by 'name'
slice_max(date, n = 1, with_ties = F) # if there's a tie (of date), take the first row…is often written like this:
obs %>%
group_by(name) %>% # group the rows by 'name'
slice_max(
date, # keep row per group with maximum date value
n = 1, # keep only the single highest row
with_ties = F) # if there's a tie (of date), take the first rowTypes of notes
FOR EXAMPLE: This is a boxed example
NOTE: This is a note
TIP: This is a tip.
CAUTION: This is a cautionary note.
DANGER: This is a warning.
Table of package, function, and other editorial decisions
See below some brief notes on decisions we made and our rationale. If you disagree or want to offer a new tool, please to join/start a conversation on our Github page.
| Decision | Considered | Outcome | Brief rationale |
|---|---|---|---|
| Epiweeks | aweek, lubridate | lubridate | consistency, package maintenance prospects |
Here the datasets used in this handbook will be described and will be “downloadable” via link (the files will be stored within the HTML, so available offline as well)
Editor-in-Chief: Neale Batra (neale.batra@gmail.com)
Editorial core team: …
Authors: …
Reviewers: …
Advisors …
Data contributors:
outbreaks package
Some of this material comes from the R4Epis website, which was also made by some of the same people…
RECON packages
Photo credits (logo): CDC Public Image gallery; R Graph Gallery
This section is not meant as a comprehensive “how to learn R” tutorial. However, it does cover some of the fundamentals that can be good to reference or refresh.
More comprehensive tutorials are available online. See the tab on recommended training.
How to install R
Visit this website https://www.r-project.org/ and download the latest version of R suitable for your computer.
How to install R Studio
Visit this website https://rstudio.com/products/rstudio/download/ and download the latest free Desktop version of RStudio suitable for your computer.
How to update R and RStudio
Other things you may need to install:
Pandoc is a document converter, a separate software from R. It comes bundled with RStudio. It helps the process of converting Rmarkdown documents to formats like .pdf and adding complex functionality.
RTools is a collection of software for building packages for R
Install from this website: https://cran.r-project.org/bin/windows/Rtools/
First, open RStudio. As their icons can look very similar, be sure you are opening RStudio and not R.
For RStudio to function you must also have R installed on the computer (see this section for installation instructions).
RStudio is an interface (GUI) for easier use of R. You can think of R as being the engine of a vehicle, doing the crucial work, and RStudio as the body of the vehicle (with seats, accessories, etc.) that helps you actually use the engine to move forward!
By default RStudio displays four rectangle panes.
TIP: If your RStudio displays only one left pane it is because you have no scripts open yet.
The R Console Pane
The R Console, by default the left or lower-left pane in R Studio, is the home of the R “engine”. This is where the commands are actually run and non-graphic outputs and error/warning messages appear. You can directly enter and run commands in the R Console, but realize that these commands are not saved as they are when running commands from a script.
If you are familiar with Stata, the R Console is like the Command Window and also the Results Window.
The Source Pane
This pane, by default in the upper-left, is space to edit and run your scripts. This pane can also display datasets (data frames) for viewing.
For Stata users, this pane is similar to your Do-file and Data Editor windows.
The Environment Pane
This pane, by default the upper-right, is most often used to see brief summaries of objects in the R Environment in the current session. These objects could include imported, modified, or created datasets, parameters you have defined (e.g. a specific epi week for the analysis), or vectors or lists you have defined during analysis (e.g. names of regions). Click on the arrow next to a dataframe name to see its variables.
In Stata, this is most similar to Variables Manager window.
Plots, Packages, and Help Pane
The lower-right pane includes several tabs including plots (display of graphics including maps), help, a file library, and available R packages (including installation/update options).
This pane contains the Stata equivalents of the Plots Manager and Project Manager windows.
Change RStudio settings and appearance in the Tools drop-down menu, by selecting Global Options
Scripts are a fundamental part of programming. Storing your code in a script (vs. typing in the console) has many advantages:
Rmarkdown is a type of script in which the script itself becomes a document (PDF, Word, HTML, Powerpoint, etc.). See the handbook page on Rmarkdown documents.
There is no difference between writing in a Rmarkdown vs an R notebook. However the execution of the document differs slightly. See this site for more details.
Shiny apps are contained within one script, which must be named app.R. This file has three components:
shinyApp functionSee the handbook page on Shiny basics, or this online tutorial: Shiny tutorial
In older versions, the above file was split into two files (ui.R and server.R)
These tabs cover how to use R working directories, and how this changes when you are working within an R project. The working directory is the root file location used by R for your work.
By default, it will save new files and outputs to this location, and will look for files to import (e.g. datasets) here as well.
NOTE: If using an [R project](#rproject), the working directory will default to the R project root folder **IF** you open RStudio by clicking open the R project (the file with .rproj extension))
Use the command setwd() with the filepath in quotations, for example: setwd("C:/Documents/R Files")
CAUTION: If using an RMarkdown script be aware of the following:
In an R Markdown script, the default working directory is the folder the Rmarkdown file (.Rmd) is saved to. If you want to change this, you can use setwd() as above, but know the change will only apply to that specific code chunk.
To change the working directory for all code chunks in an R markdown, edit the setup chunk to add the root.dir = parameter, such as below:
Setting your working directory manually (point-and-click)
From RStudio click: Session / Set Working Directory / Choose Directory (you will have to do this each time you open RStudio)
How things change in an R project
Everything in R is an object. These sections will explain:
<-)Everything you store in R - datasets, variables, a list of village names, a total population number, even outputs such as graphs - are objects which are assigned a name and can be referenced in later commands.
An object exists when you have assigned it a value (see the assignment section below). When it is assigned a value, the object appears in the Environment (see the upper right pane of RStudio). It can then be operated upon, manipulated, changed, and re-defined.
<-)Create objects by assigning them a value with the <- operator.
You can think of the assignment operator <- as the words “is defined as”. Assignment commands generally follow a standard order:
object_name <- value (or process/calculation that produce a value)
EXAMPLE: You may want to record the current epidemiological reporting week as an object for reference in later code. In this example, the object
reporting_weekis created when it is assigned the character value"2018-W10"(the quote marks make these a character value).
The objectreporting_weekwill then appear in the RStudio Environment pane (upper-right) and can be referenced in later commands.
See the R commands and their output in the boxes below.
reporting_week <- "2018-W10" # this command creates the object reporting_week by assigning it a value
reporting_week # this command prints the current value of reporting_week object in the console
## [1] "2018-W10"NOTE: Note the [1] in the R console output is simply indicating that you are viewing the first item of the output
CAUTION: An object’s value can be over-written at any time by running an assignment command to re-define its value. Thus, the order of the commands run is very important.
The following command will re-define the value of reporting_week:
reporting_week <- "2018-W51" # assigns a NEW value to the object reporting_week
reporting_week # prints the current value of reporting_week in the console
## [1] "2018-W51"Datasets are also objects and must be assigned names when they are imported.
In the code below, the object linelist is created and assigned the value of a CSV file imported with the rio package.
# linelist is created and assigned the value of the imported CSV file
linelist <- rio::import("my_linelist.csv")You can read more about importing and exporting datasets with the section on importing data.
CAUTION: A quick note on naming of objects:
Objects can be a single piece of data (e.g. my_number <- 24), or they can consist of structured data.
The graphic below, sourced from this online R tutorial shows some common data structures and their names. Not included in this image is spatial data, which is discussed in the GIS section.
In epidemiology (and particularly field epidemiology), you will most commonly encounter data frames and vectors:
| Common structure | Explanation | Example |
|---|---|---|
| Vectors | A container for a sequence of singular objects, all of the same class (e.g. numeric, character). | “Variables” (columns) in data frames are vectors (e.g. the variable age_years). |
| Data Frames | Vectors (e.g. columns) that are bound together that all have the same number of rows. | linelist is a data frame. |
Note that to create a vector that “stands alone”, or is not part of a data frame (such as a list of location names), the function c() is often used:
list_of_names <- c("Ruhengeri", "Gisenyi", "Kigali", "Butare")
All the objects stored in R have a class which tells R how to handle the object. There are many possible classes, but common ones include:
| Class | Explanation | Examples |
|---|---|---|
| Character | These are text/words/sentences “within quotation marks”. Math cannot be done on these objects. | “Character objects are in quotation marks” |
| Numeric | These are numbers and can include decimals. If within quotation marks the will be considered character. | 23.1 or 14 |
| Integer | Numbers that are whole only (no decimals) | -5, 14, or 2000 |
| Factor | These are vectors that have a specified order or hierarchy of values | Variable msf_involvement with ordered values N, S, SUB, and U. |
| Date | Once R is told that certain data are Dates, these data can be manipulated and displayed in special ways. See the page on Dates for more information. | 2018-04-12 or 15/3/1954 or Wed 4 Jan 1980 |
| Logical | Values must be one of the two special values TRUE or FALSE (note these are not “TRUE” and “FALSE” in quotation marks) | TRUE or FALSE |
| data.frame | A data frame is how R stores a typical dataset. It consists of vectors (columns) of data bound together, that all have the same number of observations (rows). | The example AJS dataset named linelist_raw contains 68 variables with 300 observations (rows) each. |
You can test the class of an object by feeding it to the function class(). Note: you can reference a specific column within a dataset using the $ notation to separate the name of the dataset and the name of the column.
class(linelist$age) # class should be numeric
## [1] "numeric"
class(linelist$gender) # class should be character
## [1] "character"Often, you will need to convert objects or variables to another class.
| Function | Action |
|---|---|
as.character() |
Converts to character class |
as.numeric() |
Converts to numeric class |
as.integer() |
Converts to integer class |
as.Date() |
Converts to Date class - Note: see section on dates for details |
as.factor() |
Converts to factor - Note: re-defining order of value levels requires extra arguments |
Here is more online material on classes and data structures in R.
$)Vectors within a data frame (variables in a dataset) can be called, referenced, or created using the $ symbol. The $ symbol connects the name of the column to the name of its data frame. The $ symbol must be used, otherwise R will not know where to look for or create the column.
In this handbook, we use the word “column” instead of “variable”.
# Retrieve the length of the vector age_years
length(linelist$age) # (age is a variable in the linelist data frame)By typing the name of the data frame followed by $ you will also see a list of all variables in the data frame. You can scroll through them using your arrow key, select one with your Enter key, and avoid spelling mistakes!
ADVANCED TIP: Some more complex objects (e.g. an epicontacts object may have multiple levels which can be accessed through multiple dollar signs. For example epicontacts$linelist$date_onset) .
[])[])
You may need to view parts of objects, which is often done using the square brackets [ ].
To view specific rows and columns of a dataset, you can do this using the syntax dataframe[rows, columns]:
# View a specific row (2) from dataset, with all columns
linelist[2,]
# View all rows, but just one column
linelist[, "date_onset"]
# View values from row 2 and columns 5 through 10
linelist[2, 5:10]
# View values from row 2 and columns 5 through 10 and 18
linelist[2, c(5:10, 18)]
# View rows 2 through 20, and specific columns
linelist[2:20, c("date_onset", "outcome", "age")]
# View rows and columns based on criteria
# *** Note the dataframe must still be names in the criteria!
linelist[linelist$age > 25 , c("date_onset", "date_birth", "age")]
# Use View() to see the outputs in the RStudio Viewer pane (easier to read)
# *** Note the capital "V" in View() function
View(linelist[2:20, "date_onset"])
# Save as a new object
new_table <- linelist[2:20, c("date_onset")] The square brackets also work to call specific parts of an object, such as output of a summary() function, or a vector:
This section on functions explains:
A function is like a machine that receives inputs, does some action with those inputs, and produces an output.
What the output is depends on the function.
Functions typically operate upon some object placed within the function’s parentheses. For example, the function sqrt() calculates the square root of a number:
Functions can also be applied to variables in a dataset. For example, when the function summary() is applied to the numeric variable age in the dataset linelist (what’s the $ symbol?), the output is a summary of the variable’s numeric and missing values.
summary(linelist$age)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.00 6.00 13.00 15.14 22.00 66.00 88NOTE: Behind the scenes, a function represents complex additional code that has been wrapped up for the user into one easy command.
Functions often ask for several inputs, called arguments, located within the parentheses of the function, usually separated by commas.
For example, this
age_pyramid()command produces an age pyramid graphic based on defined age groups and a binary split variable, such asgender. The function is given three arguments within the parentheses, separated by commas. The values supplied to the arguments establishlinelistas the data frame to use,age_groupas the variable to count, andgenderas the binary variable to use for splitting the pyramid by color.
NOTE: For this example, in the background we have created a new variable called “age_group”. To learn how to create new variable see that section of this handbook
# Creates an age pyramid by specifying the dataframe, age group variable, and a variable to split the pyramid
apyramid::age_pyramid(data = linelist, age_group = "age_group", split_by = "gender")The first half of an argument assignment (e.g.
data =) does not need to be specified if the arguments are written in a specific order (specified in the function’s documentation). The below code produces the exact same pyramid as above, because the function expects the argument order: data frame,age_groupvariable,split_byvariable.
# This command will produce the exact same graphic as above
apyramid::age_pyramid(linelist, "age_group", "gender")A more complex age_pyramid() command might include the optional arguments to:
proportional = TRUE when the default is FALSE)pal = is short for “palette” and is supplied with a vector of two color names. See the objects page for how the function c() makes a vector)NOTE: For arguments specified with an equals symbol (e.g. coltotals = ...), their order among the arguments is not important (must still be within the parentheses and separated by commas).
Packages contain functions.
On installation, R contains “base” functions that perform common elementary tasks. But many R users create specialized functions, which are verified by the R community and which you can download as a package for your own use.
One of the more challenging aspects of R is that there are often many functions or packages to choose from to complete a given task.
Functions are contained within packages which can be downloaded (“installed”) to your computer from the internet. Once a package is downloaded, you access its functions by loading the package with the library() command at the beginning of each R session.
NOTE: While you only have to install a package once, you must load it at the beginning of every R session using library() command, or an alternative like pacman’s p_load() function.
Think of R as your personal library: When you download a package your library gains a book of functions, but each time you want to use a function in that book, you must borrow that book from your library.
For clarity in this handbook, functions are usually preceeded by the name of their package using the :: symbol in the following way:
package_name::function_name()
Once a package is loaded for a session, this explicit style is not necessary. One can just use function_name(). However giving the package name is useful when a function name is common and may exist in multiple packages (e.g. plot()).
Using the package name will also load the package if it is not already loaded.
# This command uses the package "rio" and its function "import()" to import a dataset
linelist <- rio::import("linelist.xlsx", which = "Sheet1")Dependencies
Packages often depend on other packages, and these are called “dependencies”. When a package is installed from CRAN, it will typically also install its dependenices.
To read more about a function, you can try searching online for resources OR search in the Help tab of the lower-right RStudio pane.
%>%)%>%)
Two general approaches to R coding are:
Simply explained, the pipe operator (%>%) passes an intermediate output from one function to the next.
You can think of it as saying “then”. Many functions can be linked together with %>%.
Piping emphasizes a sequence of actions, not the object the actions are being performed on
Best when a sequence of actions must be performed on one object
From magrittr. Included in dplyr and tidyverse
Makes code more clean and easier to read, intuitive
Express a sequence of operations
The object is altered and then passed on to the next function
Example:
# A fake example of how to bake a care using piping syntax
cake <- flour %>% # to define cake, start with flour, and then...
left_join(eggs) %>% # add eggs
left_join(oil) %>% # add oil
left_join(water) %>% # add water
mix_together(utensil = spoon, minutes = 2) %>% # mix together
bake(degrees = 350, system = "fahrenheit", minutes = 35) %>% # bake
let_cool() # let it cool downhttps://cfss.uchicago.edu/notes/pipes/#:~:text=Pipes%20are%20an%20extremely%20useful,code%20and%20combine%20multiple%20operations.
Piping is not a base function. To use piping, the dplyr package must be installed and loaded. Near the top of every template script is a code chunk that installs and loads the necessary packages, including dplyr. You can read more about piping in the documentation.
CAUTION: Remember that even when using piping to link functions, if the assignment operator (<-) is present, the object to the left will still be over-written (re-defined) by the right side.
%<>%
This is an “assignment pipe” from the magritter package, which pipes an object forward and also re-defines the object. It must be the first pipe operator in the chain. It is shorthand, so object %<>% function() %>% function() is the same as object <- object %>% function() %>% function().
Better if:
as changes are made - still handy to know
Risks: creating new objects for each step - lots of objects. If you use the wrong one you might not know. naming can be confusing, errors not easily detectable
either name each intermediate object, or overwrite the original, or combine all the functions together. all come with risks
https://style.tidyverse.org/pipes.html
# a fake example of how to bake a cake using this method (defining intermediate objects)
batter_1 <- left_join(flour, eggs)
batter_2 <- left_join(batter_1, oil)
batter_3 <- left_join(batter_2, water)
batter_4 <- mix_together(object = batter_3, utensil = spoon, minutes = 2)
cake <- bake(batter_4, degrees = 350, system = "fahrenheit", minutes = 35)
cake <- let_cool(cake)Combine all functions together - also difficult to read
This section details operators in R, such as:
%in% operator<-
The basic assignment operator in R is <-. Such that object_name <- value (see R Basics tab on “Defining an Object”).
This assignment operator can also be written as =. We advise use of <- for general R use.
We also advise surrounding operators with spaces, for readability.
<<-
If writing functions (LINK TO PAGE), or using R in an interactive way with sourced scripts (LINK TO PAGE), then you may need to use this assignment operator <<- (base R). This operator is used to define an object in a higher ‘parent’ R Environment (LINK to tab on R environments). Also see this online reference.
%<>%
This is an “assignment pipe” from the magritter package, which pipes an object forward and also re-defines the object. It must be the first pipe operator in the chain. It is shorthand, so object %<>% function() %>% function() is the same as object <- object %>% function() %>% function().
%<+%
Used to add data to phylogenetic trees with the ggtree package. See the (LINK TO PAGE) or this online resource book.
Relational operators compare values and are often used when defining new variables and subsets of datasets. Here are the common relational operators in R:
| Function | Operator | Example | Example Result |
|---|---|---|---|
| Equal to | == |
"A" == "a" |
FALSE (because R is case sensitive) Note that == (double equals) is different from = (single equals), which acts like the assignment operator <- |
| Not equal to | != |
2 != 0 |
TRUE |
| Greater than | > |
4 > 2 |
TRUE |
| Less than | < |
4 < 2 |
FALSE |
| Greater than or equal to | >= |
6 >= 4 |
TRUE |
| Less than or equal to | <= |
6 <= 4 |
FALSE |
| Value is missing | is.na() |
is.na(7) |
FALSE (see section on missing values) |
| Value is not missing | !is.na() |
!is.na(7) |
TRUE |
Logical operators, such as AND and OR, are often used to connect relational operators and create more complicated criteria. Complex statements might require parentheses ( ) for grouping and order of application.
| Function | Operator |
|---|---|
| AND | & |
| OR | | (vertical bar) |
| Parentheses | ( ) Used to group criteria together and clarify order |
For example, below, we have a linelist with two variables we want to use to create our case definition, hep_e_rdt, a test result and other_cases_in_hh, which will tell us if there are other cases in the household. The command below uses the function case_when() to create the new variable case_def such that:
linelist_cleaned <- linelist_cleaned %>%
mutate(case_def = case_when(
is.na(hep_e_rdt) & is.na(other_cases_in_hh) ~ NA_character_,
hep_e_rdt == "Positive" ~ "Confirmed",
hep_e_rdt != "Positive" & other_cases_in_hh == "Yes" ~ "Probable",
TRUE ~ "Suspected"
))| Criteria in example above | Resulting value in new variable “case_def” |
|---|---|
If the value for variables hep_e_rdt and other_cases_in_hh are missing |
NA (missing) |
If the value in hep_e_rdt is “Positive” |
“Confirmed” |
If the value in hep_e_rdt is NOT “Positive” AND the value in other_cases_in_hh is “Yes” |
“Probable” |
| If one of the above criteria are not met | “Suspected” |
Note that R is case-sensitive, so “Positive” is different than “positive”…
In R, missing values are represented by the special value NA (capital letters N and A - not in quotation marks). If you import data that records missing data in another way (e.g. 99, “Missing”, or .), you may want to re-code those values to NA.
To test whether a value is NA, use the special function is.na(), which returns TRUE or FALSE.
rdt_result <- c("Positive", "Suspected", "Positive", NA) # two positive cases, one suspected, and one unknown
is.na(rdt_result) # Tests whether the value of rdt_result is NA
## [1] FALSE FALSE FALSE TRUETo DO: SECTION ON OTHER NA TYPES: NA_character, NA_real etc. SECTION ON NULL
All the operators and functions in this page is automatically available using base R.
These are often used to perform addition, division, to create new columns, etc. Below are common mathematical operators in R. Whether you put spaces around the operators is not important.
| Objective | Example in R |
|---|---|
| addition | 2 + 3 |
| subtraction | 2 - 3 |
| multiplication | 2 * 3 |
| division | 30 / 5 |
| exponent | 2^3 |
| order of operations | ( ) |
| Objective | Function |
|---|---|
| rounding | round(x, digits = n) |
| rounding | janitor::round_half_up(x, digits = n) |
| ceiling (round up) | ceiling(x) |
| floor (round down) | floor(x) |
| absolute value | abs(x) |
| square root | sqrt(x) |
| exponent | exponent(x) |
| natural logarithm | log(x) |
DANGER: R uses “banker’s rounding” which round up only if the upper number is even. Use round_half_up() from janitor to consistently round halfs up to the nearest whole number. See this
CAUTION: The functions below will by default include missing values in calculations. Missing values will result in an output of NA, unless the argument na.rm=TRUE is specified
| Objective | Function |
|---|---|
| mean (average) | mean(x, na.rm=T) |
| median | median(x, na.rm=T) |
| standard deviation | sd(x, na.rm=T) |
| quantiles* | quantile(x, probs) |
| sum | sum(x, na.rm=T) |
| minimum value | min(x, na.rm=T) |
| maximum value | max(x, na.rm=T) |
| range of numeric values | range(x, na.rm=T) |
| summmary** | summary(x) |
DANGER: If providing a vector of numbers to one of the above functions, be sure to wrap the numbers within c() .
# If supplying raw numbers to a function, wrap them in c()
mean(1, 6, 12, 10, 5, 0) # !!! INCORRECT !!!
## [1] 1
mean(c(1, 6, 12, 10, 5, 0)) # CORRECT
## [1] 5.666667*quantile(): x is the numeric vector to examine, and probs is a numeric vector with probabilities within 0 and 1.0, e.g c(0.5, 0.8, 0.85)
**summary(): gives a summary on a numeric vector including mean, median, and common percentiles
| Objective | Function | Example |
|---|---|---|
| create a sequence | seq(from, to, by) | seq(1, 10, 2) |
| repeat x, n times | rep(x, ntimes) | rep(1:3, 2) or rep(c("a", "b", "c"), 3) |
| subdivide a numeric vector | cut(x, n) | cut(linelist$age, 5) |
%in%%in%
A very helpful operator
To ask if a value is not %in%, put an exclamation mark (!) in front of the logic statement:
# to negate, put an exclamation in front
!"a" %in% my_vector
## [1] FALSE
!"h" %in% my_vector
## [1] TRUE%in% is very useful when using the dplyr function case_when() to recode values in a column. For example:
linelist <- linelist %>%
mutate(hospital = case_when(
hospital %in% c("St. Fr.", "Saint Francis") ~ "St. Francis")) # convert to correct spellingThis section describes the several ways to install a package:
From the CRAN online repository of packages
Download a ZIP file of a package, unpack it, and save it.
Download a package under development from Github repository
remotes package
This section explains:
Common errors and warnings and their solutions can be found in X section (TODO). See the handbook page on common errors and warnings.
When a command is run, the R Console may show you warning or error messages in red text.
A warning means that R has completed your command, but had to take additional steps or produced unusual output that you should be aware of.
An error means that R was not able to complete your command.
Look for clues:
The error/warning message will often include a line number for the problem.
If an object “is unknown” or “not found”, perhaps you spelled it incorrectly, forgot to call a package with library(), or forgot to re-run your script after making changes.
If all else fails, copy the error message into Google along with some key terms - chances are that someone else has worked through this already!
A few things to remember when writing commands in R, to avoid errors and warnings:
Variable_A is different from variable_AAny script (RMarkdown or otherwise) will give clues when you have made a mistake. For example, if you forgot to write a comma where it is needed, or to close a parentheses, RStudio will raise a flag on that line, on the right side of the script, to warn you.
(/images/Warnings_and_Errors.png)
Introduction to importing data
The key package we recommend for importing data is: rio. rio offers the useful function import() which can import many types of files into R.
The alternative to using rio would be to use functions from several other packages that are specific to a type of file (e.g. read.csv(), read.xlsx(), etc.). While these alternatives can be difficult to remember, always using rio::import() is relatively easy.
Optionally, the package here can be used in conjunction with rio. It locates files on your computer via relative pathways, usually within the context of an R project. Relative pathways are relative from a designated folder location, so that pathways listed in R code will not break when the script is run on a different computer.
This code chunk shows the loading of packages for importing data.
import()import()
When you import a dataset, you are doing the following:
The function import() from the package rio makes it easy to import many types of data files.
# An example:
#############
library(rio) # ensure package rio is loaded for use
# New object is defined as the imported data
my_csv_data <- import("linelist.csv") # importing a csv file
my_Excel_data <- import("observations.xlsx", which = "February") # import an Excel fileimport() uses the file’s extension (e.g. .xlsx, .csv, .dta, etc.) to appropriately import the file. Any optional arguments specific to the filetype can be supplied as well.
You can read more about the rio package in this online vignette
https://cran.r-project.org/web/packages/rio/readme/README.html
CAUTION: In the example above, the datasets are assumed to be located in the working directory, or the same folder as the script.
TO DO
import a specific range of cells skip rows, in excel and csv rio table of functions used for import/export/convert https://cran.r-project.org/web/packages/rio/vignettes/rio.html other useful function to know as backup EpiInfo SAS STATA Google Spreadsheets R files
A filepath can be provided in full (as below) or as a relative filepath (see next tab). Providing a full filepath can be fast and may be the best if referencing files from a shared/network drive).
The function import() (from the package rio) accepts a filepath in quotes. A few things to note:
If importing a specific sheet from an Excel file, include the sheet name in the which = argument of import(). For example:
# A demonstration showing how to import a specific Excel sheet
my_data <- rio::import("my_excel_file.xlsx", which = "Sheetname")If using the here() method to provide a relative pathway to import(), you can still indicate a specific sheet by adding the which = argument after the closing parenthese of the here() function.
You can import data manually via one of these methods:
file.choose() (leaving the parentheses empty) to trigger appearance of a pop-up window that allows the user to manually select the file from their computer. For example:# A demonstration showing manual selection of a file. When this command is run, a POP-UP window should appear.
# The filepath of the selected file will be supplied to the import() command.
my_data <- rio::import(file.choose())TIP: The pop-up window may appear BEHIND your RStudio window.
here())here())
Relative filepaths differ from static filepaths in that they are relative from a R project root directory. For example:
import("C:/Users/nsbatra/My Documents/R files/epiproject/data/linelists/ebola_linelist.xlsx")
import(here("data", "linelists", "ebola_linelist.xlsx"))
The package here and it’s function here() facilitate relative pathways.
here() works best within R projects. When the here package is first loaded (library(here)), it automatically considers the top-level folder of your R project as “here” - a benchmark for all other files in the project.
Thus, in your script, if you want to import or reference a file saved in your R project’s folders, you use the function here() to tell R where the file is in relation to that benchmark.
If you are unsure where “here” is set to, run the function here() with the empty brackets:
Below is an example of importing the file “fluH7N9_China_2013.csv” which is located in the benchmark “here” folder. All you have to do is provide the name of the file in quotes (with the appropriate ending).
If the file is within a subfolder - let’s say a “data” folder - write these folder names in quotes, separated by commas, as below:
Using the here() command produces a character filepath, which can then processed by the import() function.
# the filepath
here("data", "fluH7N9_China_2013.csv")
# the filepath is given to the import() function
linelist <- import(here("data", "fluH7N9_China_2013.csv"))NOTE: You can still import a specific sheet of an excel file as noted in the Excel tab. The here() command only supplies the filepath.
Code from WHO API Other live online data sources? TBD
Setting up to auto-import data stored on a website
TBD
Sometimes, you may want to avoid importing a row of data (e.g. the column names, which are row 1).
you can do this with the argument skip = if using import() from the rio package on a .xlsx or .csv file. Provide the number of rows you want to skip.
Unfortunately skip = only accepts one integer value, not a range (e.g. “2:10”). To skip import of specific rows that are not consecutive from the top, consider importing multiple times and using bind_rows() from dplyr. See the example below of skipping only row 2.
Your data may have a second row of data, for example if it is a “data dictionary” row (see example below).
This situation can be problematic because it can result in all columns being imported as class “character”. To solve this, you will likely need to import the data twice.
The exact arguments used to bind the correct column names depends on the type of data file (.csv, .tsv, .xlsx, etc.). If using rio’s import() function, understand which function rio uses to import your data, and then give the appropriate argument to skip lines and/or designate the column names. See the handbook page on importing data (LINK) for details on rio.
For Excel files:
# For excel files (remove 2nd row)
linelist_raw_names <- import("linelist_raw.xlsx") %>% names() # save true column names
# import, skip row 2, assign to col_names =
linelist_raw <- import("linelist_raw.xlsx", skip = 2, col_names = linelist_raw_names) For CSV files:
# For csv files
linelist_raw_names <- import("linelist_raw.csv") %>% names() # save true column names
# note argument is 'col.names ='
linelist_raw <- import("linelist_raw.csv", skip = 2, col.names = linelist_raw_names) Backup option - changing column names as a separate command
# assign/overwrite headers using the base 'colnames()' function
colnames(linelist_raw) <- linelist_raw_namesBonus! If you do have a second row that is a data dictionary, you can easily create a proper data dictionary from it using the gather() command from the tidyr package.
source: https://alison.rbind.io/post/2018-02-23-read-multiple-header-rows/
TO DO
Since a data frame is a combination of vertical vectors (columns), R by default expects manual entry of data to also be in vertical vectors (columns).
# define each vector (vertical column) separately, each with its own name
PatientID <- c(235, 452, 778, 111)
Treatment <- c("Yes", "No", "Yes", "Yes")
Death <- c(1, 0, 1, 0)CAUTION: All vectors must be the same length (same number of values).
The vectors can then be bound together using the function data.frame():
# combine the columns into a data frame, by referencing the vector names
manual_entry_cols <- data.frame(PatientID, Treatment, Death)And now we display the new dataset:
Use the tribble function from the tibble package from the tidverse (onlinetibble reference).
Note how column headers start with a tilde (~). Also note that each column must contain only one class of data (character, numeric, etc.).
You can use tabs, spacing, and new rows to make the data entry more intuitive and readable. For example:
# create the dataset manually by row
manual_entry_rows <- tibble::tribble(
~colA, ~colB,
"a", 1,
"b", 2,
"c", 3
)And now we display the new dataset:
OR ADD ROWS dplyr TO DO
If you copy data from elsewhere and have it on your clipboard, you can try the following command to convert those data into an R data frame:
Working with dates in R is notoriously difficult when compared to other object classes. R often interprets dates as character objects - this means they cannot be used for general date operations such as making time series and calculating time intervals. To make matters more difficult, there are many date formats, some of which can be confused for other formats. Luckily, dates can be wrangled easily with practice, and with a set of helpful packages.
Dates in R are their own class of object - the Date class. It should be noted that there is also a class that stores objects with date and time. Date time objects are formally referred to as and/or POSIXt, POSIXct, and/or POSIXlt classes (the difference isn’t important). These objects are informally referred to as datetime classes.
You can get the system date or system datetime by doing the following:
# get the system date - this is a DATE class
Sys.Date()
## [1] "2021-01-23"
# get the system time - this is a DATETIME class
Sys.time()
## [1] "2021-01-23 14:15:08 EST"The following packages are recommended for working with dates:
# Checks if package is installed, installs if necessary, and loads package for current session
pacman::p_load(aweek, # flexibly converts dates to weeks, and vis-versa
lubridate, # for conversions to months, years, etc.
linelist, # function to guess messy dates
ISOweek) # another option for creating weeksThe standard, base R function to convert an object or variable to class Date is as.Date() (note capitalization).
as.Date() requires that the user specify the existing* format of the date*, so it can understand, convert, and store each element (day, month, year, etc.) correctly. Read more online about as.Date().
If used on a variable, as.Date() therefore requires that all the character date values be in the same format before converting. If your data are messy, try cleaning them or consider using guess_dates() from the linelist package.
It can be easiest to first convert the variable to character class, and then convert to date class:
as.character()as.Date()Within the as.Date() function, you must use the format= argument to tell R the current format of the date components - which characters refer to the month, the day, and the year, and how they are separated. If your values are already in one of R’s standard date formats (YYYY-MM-DD or YYYY/MM/DD) the format= argument is not necessary.
For example, if your character dates are in the format DD/MM/YYYY, like “24/04/1968”, then your command to turn the values into dates will be as below. Putting the format in quotation marks is necessary.
TIP: The format = argument is not telling R the format you want the dates to be, but rather how to identify the date parts as they are before you run the command.
TIP:Be sure that in the format = argument you use the date-part separator (e.g. /, -, or space) that is present in your dates.
Conveting character objects to dates can be made far easier by using the lubridate package. The lubridate package is a tidyverse package designed to make working with dates and time more simple and consistent than in base R. For these reasons, lubridate is often considered the gold-standard package for dates and time, and is recommended whenever working with them.
The lubridate package provides a number of different helper functions designed to convert character objects to dates in an intuitive, and more lenient way than specifying the format in as.Date(). These functions are specific to the rough date format, but allow for a variety of separators, and synonyms for dates (e.g. 01 vs Jan vs January) - they are named after abbreviations of date formats.
# load packages
library(lubridate)
# read date in year-month-day format
ymd("2020-10-11")
## [1] "2020-10-11"
ymd("20201011")
## [1] "2020-10-11"
# read date in month-day-year format
mdy("10/11/2020")
## [1] "2020-10-11"
mdy("Oct 11 20")
## [1] "2020-10-11"
# read date in day-month-year format
dmy("11 10 2020")
## [1] "2020-10-11"
dmy("11 October 2020")
## [1] "2020-10-11"If using piping and the tidyverse, the converting a character column to dates might look like this:
Once complete, you can run a command to verify the class of the variable
Once the values are in class Date, R will by default display them in the standard format, which is YYYY-MM-DD.
datetime classesAs previously mentioned, R also supports a datetime class - a variable that contains date and time information. As with the Date class, these often need to be converted from character objects to datetime objects.
A standard datetime object is formatted with the date first, which is followed by a time component - for example 01 Jan 2020, 16:30. As with dates, there are many ways this can be formatted, and there are numerous levels of precision (hours, minutes, seconds) that can be supplied. Luckily, lubridate helper functions also exist to help convert these strings to datetime objects. These functions are the same as the date helper functions, with _h (only hours supplied), _hm (hours and minutes supplied), or _hms (hours, minutes, and seconds supplied) appended to the end (e.g. dmy_hms()). These can be used as shown:
# convert datetime with only hours to datetime object
ymd_h("2020-01-01 16hrs")
## [1] "2020-01-01 16:00:00 UTC"
ymd_h("2020-01-01 4PM")
## [1] "2020-01-01 16:00:00 UTC"
# convert datetime with hours and minutes to datetime object
dmy_hm("Jan 1st 2020 16:20")
## [1] NA
# convert datetime with hours, minutes, and seconds to datetime object
mdy_hms("01 January 20, 16:20:40")
## [1] NA
# you can supply time zone but it is ignored
mdy_hms("01 January 20, 16:20:40 PST")
## [1] NAWhen working with a linelist, time and date columns can be combined to create a datetime column using these functions:
# time_admission is a variable in hours:minutes
linelist_cleaned <- linelist_cleaned %>%
# assume that when time of admission is not given, it the median admission time
mutate(
time_admission_clean = ifelse(
is.na(time_admission),
median(time_admission),
time_admission
) %>%
# use paste0 to combine two columns to create a character vector, and use ymd_hm() to convert to datetime
mutate(
date_time_of_admission = paste0(
date_hospitalisation, time_admission_clean, sep = " "
) %>% ymd_hm()
)lubridate can also be used for a variety of other functions, such as extracting aspects of a date/datetime, performing date arithmetic, or calculating date intervals
# extract the month from this date
example_date <- ymd("2020-03-01")
# extract the month and year from this date
month(example_date)
## [1] 3
year(example_date)
## [1] 2020
# get the epiweek of this date (this will be expanded later)
epiweek(example_date)
## [1] 10
# get the day of the week for this date (this will be expanded later)
wday(example_date)
## [1] 1
# add 3 days to this date
example_date + days(3)
## [1] "2020-03-04"
# add 7 weeks and subtract two days from this date
example_date + weeks(7) - days(2)
## [1] "2020-04-17"
# find the interval between this date and Feb 20 2020
example_date - ymd("2020-02-20")
## Time difference of 10 daysThis can all be brought together to work with data - for example:
library(lubridate)
linelist_cleaned <- linelist_cleaned %>%
# convert date of onset from character to date objects by specifying dmy format
mutate(date_of_onset = dmy(date_of_onset),
date_of_hospitalisation = dmy(date_of_hospitalisation)) %>%
# filter out all cases without onset in march
filter(month(date_of_onset) == 3) %>%
# find the difference in days between onset and hospitalisation
mutate(onset_to_hosp_days = date_of_hospitalisation - date_of_onset)guess_dates()The function guess_dates() attempts to read a “messy” date variable containing dates in many different formats and convert the dates to a standard format. You can read more online about guess_dates(), which is in the linelist package.
For example:
guess_dateswould see the following dates “03 Jan 2018”, “07/03/1982”, and “08/20/85” and convert them in the class Date to: 2018-01-03, 1982-03-07, and 1985-08-20.
linelist::guess_dates(c("03 Jan 2018", "07/03/1982", "08/20/85")) # guess_dates() not yet available on CRAN for R 4.0.2
# try install via devtools::install_github("reconhub/linelist")Some optional arguments for guess_dates() that you might include are:
error_tolerance - The proportion of entries which cannot be identified as dates to be tolerated (defaults to 0.1 or 10%)last_date - the last valid date (defaults to current date)first_date - the first valid date. Defaults to fifty years before the last_date.Excel stores dates as the number of days since December 30, 1899. If the dataset you imported from Excel shows dates as numbers or characters like “41369”… use the as.Date() or as_date() function to convert, but instead of supplying a format as above, supply an origin date. This will not work if the excel date is read as a character type, so be sure to ensure the date is a numeric class (or convert it to one)!
NOTE: You should provide the origin date in R’s default date format ("YYYY-MM-DD").
Once dates are the correct class, you often want them to display differently (e.g. in a plot, graph, or table). For example, to display as “Monday 05 Jan” instead of 2018-01-05. You can do this with the function format(), which works in a similar way as as.Date(). Read more in this online tutorial. Remember that the output from format() is a character type, so is generally used for display purposes only!
%d = Day # (of the month e.g. 16, 17, 18…) %a = abbreviated weekday (Mon, Tues, Wed, etc.)
%A = full weekday (Monday, Tuesday, etc.)
%m = # of month (e.g. 01, 02, 03, 04)
%b = abbreviated month (Jan, Feb, etc.)
%B = Full Month (January, February, etc.)
%y = 2-digit year (e.g. 89)
%Y = 4-digit year (e.g. 1989)
%h = hours (24-hr clock)
%m = minutes
%s = seconds %z = offset from GMT
%Z = Time zone (character)
An example of formatting today’s date:
# today's date, with formatting
format(Sys.Date(), format = "%d %B %Y")
## [1] "23 January 2021"
# easy way to get full date and time (no formatting)
date()
## [1] "Sat Jan 23 14:15:11 2021"
# formatted date, time, and time zone (using paste0() function)
paste0(
format(Sys.Date(), format = "%A, %b %d '%y, %z %Z, "),
format(Sys.time(), format = "%H:%M:%S")
)
## [1] "Saturday, Jan 23 '21, +0000 UTC, 14:15:11"The difference between dates can be calculated by:
# define variables as date classes
date_of_onset <- ymd("2020-03-16")
date_lab_confirmation <- ymd("2020-03-20")
# find the delay between onset and lab confirmation
days_to_lab_conf <- as.double(date_lab_confirmation - date_of_onset)
days_to_lab_conf
## [1] 4In a dataframe format (i.e. when working with a linelist), if either of the above dates is missing, the operation will fail for that row. This will result in an NA instead of a numeric value. When using this column for calculations, be sure to set the na.rm option to TRUE. For example:
# add a new column
# calculating the number of days between symptom onset and patient outcome
linelist_delay <- linelist_cleaned %>%
mutate(
days_onset_to_outcome = as.double(date_of_outcome - date_of_onset)
)
# calculate the median number of days to outcome for all cases where data are available
med_days_outcome <- median(linelist_delay$dats_onset_to_outcome, na.rm = T)
# often this operation might be done only on a subset of data cases, e.g. those who died
# this is easy to look at and will be explained later in the handbookWhen data is present in different time time zones, it can often be important to standardise this data in a unified time zone. This can present a further challenge, as the time zone component of data must be coded manually in most cases.
In R, each datetime object has a timezone component. By default, all datetime objects will carry the local time zone for the computer being used - this is generally specific to a location rather than a named timezone, as time zones will often change in locations due to daylight savings time. It is not possible to accurately compensate for time zones without a time component of a date, as the event a date variable represents cannot be attributed to a specific time, and therefore time shifts measured in hours cannot be reasonably accounted for.
To deal with time zones, there are a number of helper functions in lubridate that can be used to change the time zone of a datetime object from the local time zone to a different time zone. Time zones are set by attributing a valid tz database time zone to the datetime object. A list of these can be found here - if the location you are using data from is not on this list, nearby large cities in the time zone are available and serve the same purpose.
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
# assign the current time to a variable
time_now <- Sys.time()
time_now
## [1] "2021-01-23 14:15:11 EST"
# use with_tz() to assign a new timezone to the variable, while CHANGING the clock time
time_london_real <- with_tz(time_now, "Europe/London")
# use force_tz() to assign a new timezone to the variable, while KEEPING the clock time
time_london_local <- force_tz(time_now, "Europe/London")
# note that as long as the computer that was used to run this code is NOT set to London time, there will be a difference in the times (the number of hours difference from the computers time zone to london)
time_london_real - time_london_local
## Time difference of 5 hoursThis may seem largely abstract, and is often not needed if the user isn’t working across time zones. One simple example of its implementation is:
The templates use the very flexible package aweek to set epidemiological weeks. You can read more about it on the RECON website
See the section on epicurves.
Sys.Date( ) returns the current date of your computerSys.Time() returns the current time of your computerdate() returns the current date and time.This page demonstrates common steps necessary to clean a dataset. It uses a simulated Ebola case linelist, which is used throughout the handbook.
replace missing with dealing with cases (all lower, etc) case_when() factors
In epidemiological analysis and data processing, cleaning steps are often performed together and sequentially. In R this often manifests as a cleaning “pipeline”, where the raw dataset is passed or “piped” from one cleaning step to another. The chain utilizes dplyr verbs and the magrittr pipe operator (see handbook page on dplyr and tidyverse coding style (LINK HERE). The pipe begins with the “raw” data (linelist_raw) and ends with a “clean” dataset (linelist).
In a cleaning pipeline the order of the steps is important. Cleaning steps might include:
Column names are used very often so they need to have “clean” syntax. We suggest the following:
The columns names of linelist_raw are below. We can see that there are some with spaces. We also have different naming patterns for dates (‘date onset’ and ‘infection date’).
Also note that in the raw data, the two final columns names were two merged cells with one name. The import() function used the name for the first of the two columns, and assigned the second column the name “…23” as it was then empty (referring to the 23rd column).
names(linelist_raw)
## [1] "row_num" "case_id" "generation" "infection date"
## [5] "date onset" "hosp date" "date_of_outcome" "outcome"
## [9] "gender" "hospital" "lon" "lat"
## [13] "infector" "source" "age" "ct_blood"
## [17] "age_unit" "fever" "chills" "cough"
## [21] "aches" "vomit" "time_admission" "merged_header"
## [25] "...25"Note: For a column name that include spaces, surround the name with back-ticks, for example: linelist$`infection date`. On a keyboard, the back-tick (`) is different from the single quotation mark ('), and is sometimes on the same key as the tilde (~).
The function clean_names() from the package janitor standardizes column names and makes them unique by doing the following:
case = argument (“snake” is default, alternatives include “sentence”, “title”, “small_camel”…)replace = argument (e.g. replace = c(onset = “date_of_onset”))Below, the cleaning pipeline begins by using clean_names() on the raw linelist.
# send the dataset through the function clean_names()
linelist <- linelist_raw %>%
janitor::clean_names()
# see the new names
names(linelist)
## [1] "row_num" "case_id" "generation" "infection_date"
## [5] "date_onset" "hosp_date" "date_of_outcome" "outcome"
## [9] "gender" "hospital" "lon" "lat"
## [13] "infector" "source" "age" "ct_blood"
## [17] "age_unit" "fever" "chills" "cough"
## [21] "aches" "vomit" "time_admission" "merged_header"
## [25] "x25"NOTE: The column name “…25” was changed to “x25”.
Re-naming columns manually is often necessary. Below, re-naming is performed using the rename() function from the dplyr package, as part of a pipe chain. rename() uses the style “NEW = OLD”, the new column name is given before the old column name.
Below, a re-name command is added to the cleaning pipeline:
# CLEANING 'PIPE' CHAIN (starts with raw data and pipes it through cleaning steps)
##################################################################################
linelist <- linelist_raw %>%
# standardize column name syntax
janitor::clean_names() %>%
# manually re-name columns
# NEW name # OLD name
rename(date_infection = infection_date,
date_hospitalisation = hosp_date,
date_outcome = date_of_outcome)Now you can see that the columns names have been changed:
You can also rename by column position, instead of column name, for example:
If you importing an Excel sheet with a missing column name, depending on the import function used, R will likely create a column name with a value like “…1” or “…2”. You can clean these names manually by referencing their position number (see above), or their name (linelist_raw$...1).
Merged cells in an Excel file are a common occurrence when receiving data from field level. Merged cells can be nice for human reading of data, but cause many problems for machine reading of data. R cannot accommodate merged cells.
Remind people doing data entry that human-readable data is not the same as machine-readable data. Strive to train users about the princiles of tidy data. If at all possible, try to change procedures so that data arrive in a tidy format without merged cells.
When using rio’s import() function, the value in a merged cell will be assigned to the first cell and subsequent cells will be empty.
One solution to deal with merged cells is to import the data with the function readWorkbook() from package openxlsx. Set the argument fillMergedCells = TRUE. This gives the value in a merged cell to all cells within the merge range.
DANGER: If column names are merged, you will end up with duplicate column names, which you will need to fix manually - R does not work well with duplicate column names! You can re-name them by referencing their position (e.g. column 5), as explained in the section on manual column name cleaning..
Sometimes, you may want to avoid importing a row of data (e.g. the column names, which are row 1).
you can do this with the argument skip = if using import() from the rio package on a .xlsx or .csv file. Provide the number of rows you want to skip.
Unfortunately skip = only accepts one integer value, not a range (e.g. “2:10”). To skip import of specific rows that are not consecutive from the top, consider importing multiple times and using bind_rows() from dplyr. See the example below of skipping only row 2.
Your data may have a second row of data, for example if it is a “data dictionary” row (see example below).
This situation can be problematic because it can result in all columns being imported as class “character”. To solve this, you will likely need to import the data twice.
The exact arguments used to bind the correct column names depends on the type of data file (.csv, .tsv, .xlsx, etc.). If using rio’s import() function, understand which function rio uses to import your data, and then give the appropriate argument to skip lines and/or designate the column names. See the handbook page on importing data (LINK) for details on rio.
For Excel files:
# For excel files (remove 2nd row)
linelist_raw_names <- import("linelist_raw.xlsx") %>% names() # save true column names
# import, skip row 2, assign to col_names =
linelist_raw <- import("linelist_raw.xlsx", skip = 2, col_names = linelist_raw_names) For CSV files:
# For csv files
linelist_raw_names <- import("linelist_raw.csv") %>% names() # save true column names
# note argument is 'col.names ='
linelist_raw <- import("linelist_raw.csv", skip = 2, col.names = linelist_raw_names) Backup option - changing column names as a separate command
# assign/overwrite headers using the base 'colnames()' function
colnames(linelist_raw) <- linelist_raw_namesBonus! If you do have a second row that is a data dictionary, you can easily create a proper data dictionary from it using the gather() command from the tidyr package.
source: https://alison.rbind.io/post/2018-02-23-read-multiple-header-rows/
TO DO
In some cases, you may want to combine two header rows into one. This command will define the column names as the combination (pasting together) of the existing column names with the value underneath in the first row. Replace “df” with the name of your dataset.
CAUTION: This tab may follow from previous tabs.
Often the first step of cleaning data is selecting the columns you want to work with, and to set their order in the dataframe. In a dplyr chain of verbs, this is done with select(). Note that in these examples we modify linelist with select(), but do not assign/overwrite. We just display the resulting new column names, for purpose of example.
CAUTION: In the examples below, linelist is modified with select() but not over-written. New column names are only displayed for purpose of example.
Here are all the column names in the linelist:
names(linelist)
## [1] "row_num" "case_id" "generation"
## [4] "date_infection" "date_onset" "date_hospitalisation"
## [7] "date_outcome" "outcome" "gender"
## [10] "hospital" "lon" "lat"
## [13] "infector" "source" "age"
## [16] "ct_blood" "age_unit" "fever"
## [19] "chills" "cough" "aches"
## [22] "vomit" "time_admission" "merged_header"
## [25] "x25"Select only the columns you want to remain, and their order of appearance
# linelist dataset is piped through select() command, and names() prints just the column names
linelist %>%
select(case_id, date_onset, date_hospitalisation, fever) %>%
names() # display the column names
## [1] "case_id" "date_onset" "date_hospitalisation"
## [4] "fever"Indicate which columns to remove by placing a minus symbol “-” in front of the column name (e.g. select(-outcome)), or a vector of column names (as below). All other columns will be retained. Inside select() you can use normal operators such as c() to list several columns, : for consecutive columns, ! for opposite, & for AND, and | for OR.
linelist %>%
select(-c(date_onset, fever:vomit)) %>% # remove onset and all symptom columns
names()
## [1] "row_num" "case_id" "generation"
## [4] "date_infection" "date_hospitalisation" "date_outcome"
## [7] "outcome" "gender" "hospital"
## [10] "lon" "lat" "infector"
## [13] "source" "age" "ct_blood"
## [16] "age_unit" "time_admission" "merged_header"
## [19] "x25"Re-order the columns - use everything() to signify all other columns not specified in the select() command:
# move case_id, date_onset, date_hospitalisation, and gender to beginning
linelist %>%
select(case_id, date_onset, date_hospitalisation, gender, everything()) %>%
names()
## [1] "case_id" "date_onset" "date_hospitalisation"
## [4] "gender" "row_num" "generation"
## [7] "date_infection" "date_outcome" "outcome"
## [10] "hospital" "lon" "lat"
## [13] "infector" "source" "age"
## [16] "ct_blood" "age_unit" "fever"
## [19] "chills" "cough" "aches"
## [22] "vomit" "time_admission" "merged_header"
## [25] "x25"As well as everything() there are several special functions that work within select(), namely:
everything() - all other columns not mentionedlast_col() - the last columnwhere() - applies a function to all columns and selects those which are TRUEstarts_with() - matches to a specified prefix. Example: select(starts_with("date"))ends_with() - matches to a specified suffix. Example: select(ends_with("_end"))contains() - columns containing a character string. Example: select(contains("time"))matches() - to apply a regular expression (regex). Example: select(contains("[pt]al"))num_range() -any_of() - matches if column is named. Useful if the name might not exist. Example: select(any_of(date_onset, date_death, cardiac_arrest))Here is an example using where():
select() as a stand-alone commandselect() as a stand-alone command
Select can also be used as an independent command (not in a pipe chain). In this case, the first argument is the original dataframe to be operated upon.
In the linelist, there are a few columns we do not need: row_num, merged_header, and x25. Remove them by adding a select() command to the cleaning pipe chain:
# CLEANING 'PIPE' CHAIN (starts with raw data and pipes it through cleaning steps)
##################################################################################
# begin cleaning pipe chain
###########################
linelist <- linelist_raw %>%
# standardize column name syntax
janitor::clean_names() %>%
# manually re-name columns
# NEW name # OLD name
rename(date_infection = infection_date,
date_hospitalisation = hosp_date,
date_outcome = date_of_outcome) %>%
# remove column
select(-c(row_num, merged_header, x25))CAUTION: This tab may follow from previous tabs.
See section on object classes
Often you will need to set the correct class for a column. The most common approach is to use mutate() to define the column as itself, but with a different class. Generally, this looks like this:
# Examples of modifying class
linelist <- linelist %>%
mutate(date_var = as.Date(date_var, format = "MM/DD/YYYY"), # format should be the format of the raw data
numeric_var = as.numeric(numeric_var),
character_var = as.character(character_var),
factor_var = factor(factor_var, levels = c(), labels = c())
)First we run some checks on the classes of important columns.
The class of the “age” column is character. To perform analysis, we need those numbers to be recognized as numeric!
The class of the “date_onset” column is also character! To perform analysis, these dates must be recognized as dates!
However, if we try to classify the date_onset column as date, we would get an error. Use table() or sort or another method to examine all the values and identify different one. For example in our dataset we see that we see that one date_onset value was entered in a different format (15th April 2014) than all the other values!
##
## 15th April 2014 2012-05-03 2012-05-06 2012-05-08 2012-05-16
## 1 1 1 1 1
## 2012-05-21
## 1
Before we can classify “date_onset” as a date, this value must be fixed to be the same format as the others. You can fix the date in the source data, or, we can do in the cleaning pipeline via mutate() and recode(). This must be done before the commands to convert to class Date. (LINK TO DATE SECTION).
# fix incorrect values # old value # new value
mutate(date_onset = recode(date_onset, "15th April 2014" = "2014-04-15"))The mutate() line can be read as: “mutate date_onset to equal date_onset recoded so that OLD VALUE is changed to NEW VALUE”. Note that this pattern (OLD = NEW) for recode() is the opposite of most R patterns (new = old). The R development community is working on revising this for recoding.
Especially after converting to class date, check your data visually or with table() to confirm that they were converted correctly! For as.Date(), the format = argument is often a source of errors.
You can use The dplyr function across() with mutate() to convert several columns at once to a new class. across() allows you to specify which columns you want a function to apply to. Below, we want to mutate the columns where is.POSIXct() (a type of date/time class that shows unnecessary timestamps) is TRUE, and apply the function is.Date() to them, in order to convert them to class “date”.
across() we also use the function where().is.character(), is.numeric(), and is.logical()) are from base Racross() are written without the empty parentheses ()Below, the described cleaning steps are added to the pipe chain.
# CLEANING 'PIPE' CHAIN (starts with raw data and pipes it through cleaning steps)
##################################################################################
# begin cleaning pipe chain
###########################
linelist <- linelist_raw %>%
# standardize column name syntax
janitor::clean_names() %>%
# manually re-name columns
# NEW name # OLD name
rename(date_infection = infection_date,
date_hospitalisation = hosp_date,
date_outcome = date_of_outcome) %>%
# remove column
select(-c(row_num, merged_header, x25)) %>%
# ABOVE ARE UPSTREAM CLEANING STEPS ALREADY DISCUSSED
###################################################
# fix incorrect values # old value # new value
mutate(date_onset = recode(date_onset, "15th April 2014" = "2014-04-15")) %>%
# correct the class of the columns
mutate(across(contains("date"), as.Date),
generation = as.numeric(generation),
age = as.numeric(age)) See the tabs below to add columns and rows
mutate()mutate()
We advise creating new columns with dplyr functions as part of a chain of such verb functions (e.g. filter, mutate, etc.)
If in need of a stand-alone command, you can use mutate() or the base R style to create a new column (see below).
The verb mutate() is used to add a new column, or to modify an existing one. Below is an example of creating a new columns with mutate(). The syntax is: new_column_name = value or function.
It is best practice to separate each new column with a comma and new line. Below, some practice columns are created:
linelist <- linelist %>% # creating new, or modifying old dataset
mutate(new_var_dup = case_id, # new column = duplicate/copy another column
new_var_static = 7, # new column = all values the same
new_var_static = new_var_static + 5, # you can overwrite a column, and it can be a calculation using other variables
new_var_paste = stringr::str_glue("{hospital} on ({date_hospitalisation})") # new column = pasting together values from other columns
) Scroll to the right to see the new columns (first 50 rows):
TIP: The verb transmute() adds new columns just like mutate() but also drops/removes all other columns that you do not mention.
To define a new column (or re-define a column) using base R, just use the assignment operator as below. Remember that when using base R you must specify the dataframe before writing the column name (e.g. dataframe$column). Here are two dummy examples:
TO DO
Remember that each column must contain values of only one class (either character, numeric, logical, etc.). So adding a row requires nuance to maintain this.
linelist <- linelist %>%
add_row(row_num = 666, case_id = "abc", generation = 4, `infection date` = as.Date("2020-10-10"), .before = 2)use .before and .after. .before = 3 will put it before the 3rd row. Default is to add it to the end. columns not specified will be let empty. The new row number may look strange (“…23”) but the row numbers have changed. So if using the command twice examine/test carefully.
If your class is off you will see an error like this: Error: Can’t combine ..1$infection date ..2$infection date as.Date() like as.Date("2020-10-10"))
CAUTION: This tab may follow from previous tabs.
Using mutate on GROUPED dataframes https://dplyr.tidyverse.org/reference/mutate.html
Taken from website above:
#Because mutating expressions are computed within groups, they may yield different results on grouped tibbles. This will be the case as #soon as an aggregating, lagging, or ranking function is involved. Compare this ungrouped mutate:
starwars %>%
select(name, mass, species) %>%
mutate(mass_norm = mass / mean(mass, na.rm = TRUE))
With the grouped equivalent:
starwars %>%
select(name, mass, species) %>%
group_by(species) %>%
mutate(mass_norm = mass / mean(mass, na.rm = TRUE))
The former normalises mass by the global average whereas the latter normalises by the averages within species levels.# CLEANING 'PIPE' CHAIN (starts with raw data and pipes it through cleaning steps)
##################################################################################
# begin cleaning pipe chain
###########################
linelist <- linelist_raw %>%
# standardize column name syntax
janitor::clean_names() %>%
# manually re-name columns
# NEW name # OLD name
rename(date_infection = infection_date,
date_hospitalisation = hosp_date,
date_outcome = date_of_outcome) %>%
# remove column
select(-c(row_num, merged_header, x25)) %>%
# fix incorrect values # old value # new value
mutate(date_onset = recode(date_onset, "15th April 2014" = "2014-04-15")) %>%
# correct the class of the columns
mutate(across(contains("date"), as.Date),
generation = as.numeric(generation),
age = as.numeric(age)) %>%
# ABOVE ARE UPSTREAM CLEANING STEPS ALREADY DISCUSSED
###################################################
# create column: delay to hospitalisation
mutate(days_onset_hosp = as.numeric(date_hospitalisation - date_onset))For example, in linelist the values in the column “hospital” must be cleaned. There are several different spellings (often the word “Hospital” is missing an “s” and is written “Hopital”), and many missing values.
table(linelist$hospital, useNA = "always")
##
## Central Hopital Central Hospital
## 11 451
## Hospital A Hospital B
## 290 289
## Military Hopital Military Hospital
## 30 805
## Mitylira Hopital Mitylira Hospital
## 1 81
## Other Port Hopital
## 904 47
## Port Hospital St. Mark's Maternity Hospital (SMMH)
## 1770 418
## St. Marks Maternity Hopital (SMMH) <NA>
## 11 1501These tabs demonstrate re-coding values manually b providing specific spellings to be corrected:
replace() for specific rowsrecode() for entire columnsreplace()To manually change values for specific rows within a dataframe (from within a pipe chain), use replace() within mutate().
Use a logic condition to specify rows, for example an ID value of one row. The general syntax is:
mutate(col_to_change = replace(col_to_change, criteria for rows, new value)).
In the first example below, the gender value, in the row where id is “2195”, is changed to “Female”.
recode()recode()
To change spellings manually, one-by-one, you can use the recode() function *within the mutate() function. The code is saying that the column “hospital” should be defined as the current column “hospital”, but with certain changes (the syntax is OLD = NEW). Don’t forget commas!
linelist <- linelist %>%
mutate(hospital = recode(hospital,
# OLD = NEW
"Mitylira Hopital" = "Military Hospital",
"Mitylira Hospital" = "Military Hospital",
"Military Hopital" = "Military Hospital",
"Port Hopital" = "Port Hospital",
"Central Hopital" = "Central Hospital",
"other" = "Other",
"St. Marks Maternity Hopital (SMMH)" = "St. Mark's Maternity Hospital (SMMH)"
))Now we see the values in the hospital column have been corrected:
table(linelist$hospital, useNA = "always")
##
## Central Hospital Hospital A
## 462 290
## Hospital B Military Hospital
## 289 917
## Other Port Hospital
## 904 1817
## St. Mark's Maternity Hospital (SMMH) <NA>
## 429 1501TIP: The number of spaces before and after an equals sign does not matter. Make your code easier to read by aligning the = for all or most rows. Also, consider adding a hashed comment row to clarify for future readers which side is OLD and which side is NEW.
TIP: Sometimes a blank character value exists in a dataset (not recognized as R’s value for missing - NA). You can reference this value with two quotation marks with no space inbetween ("").
If necessary, you make manual changes to a specific value in a dataframe by referencing the row number of case ID. But remember it is better if you can make these changes permanently in the underlying data!
Here is a fake example. It reads as “Change the value of the dataframe linelist‘s column onset_date (for the row where linelist’s column case_id has the value ’9d4019’) to as.Date("2020-10-24")”.
These tabs demonstrate re-coding values in a column using logic and conditions:
case_when()ifelse() and if_else()replace_na()na_if()coalesnce()case_when()case_when()
If you need to use logic statements to recode values, or want to use operators like %in%, use dplyr’s case_when() instead. If you use case_when() please read the thorough explanation HERE LINK, as there are important differences from recode() in syntax and logic order!
Note that all Right-hand side (RHS) inputs must be of the same class (e.g. character, numeric, logical). Notice the use of the special value NA_real_ instead of just NA.
ifelse() and if_else()ifelse() and if_else()
For simple uses of logical re-coding or new variable creationgyou can use ifelse() or if_else(). Though in most cases it is better to use case_when().
These commands are simplified versions of an if and else statement. The general syntax is ifelse(condition, value if condition evaluates to TRUE, value if condition evaluates to FALSE). If used in a mutate(), each row is evaluated. if_else() is a special version from dplyr that handles dates in the condition.
It can be tempting to string together many ifelse commands… resist this and use case_when() instead! It is much more simple, easier to read, and easier to identify errors.
IMAGE of ifelse string with X across is.
You can reference other columns with the ifelse() function within mutate():
Example of ifelse():
Example of if_else() (using dates): Note that if the ‘true’ value is a date, the ‘false’ value must also qualify a date, hence using the special character NA_real_ instead of just NA.
Note: If you want to alternate a value used in your code based on other circumstances, consider using switch() from base R. For example if… TO DO. See the section on using switch() in the page on R interactive console.
replace_na()To change missing values (NA) to a specific character value, such as “Missing”, use the function replace_na() within mutate(). Note that this is used in the same manner as recode above - the name of the variable must be repeated within replace_na().
na_if()Likewise you can quickly convert a specific character value to NA using na_if(). The command below is the opposite of the one above. It converts any values of “Missing” to NA.
coalesce()This dplyr function finds the first non-missing value at each position. So, you provide it with columns and for each row it will fill the value with the first non-missing value in the columns you provided.
For example, you might use thiscoalesce()` create a “location” variable from hypothetical variables “patient_residence” and “reporting_jurisdiction”, where you prioritize patient residence information, if it exists.
TO DO lead(), lag() cumsum(), cummean(), cummin(), cummax(), cumany(), cumall(),
CAUTION: This tab may follow from previous tabs.
## load cleaning rules and only keep columns in mll
mll_cleaning_rules <- import(here("dictionaries/mll_cleaning_rules.xlsx")) %>%
filter(column %in% c(names(mll_raw), ".global"))
## define columns that are not cleand
unchanged <- c(
"epilink_relationship",
"narratives",
"epilink_relationship_detail"
)
mll_clean <- mll_raw %>%
## convert to tibble
as_tibble() %>%
## clean columns using cleaning rules
clean_data(
wordlists = mll_cleaning_rules,
protect = names(.) %in% unchanged
)Here we add the described cleaning steps to the pipe chain.
# CLEANING 'PIPE' CHAIN (starts with raw data and pipes it through cleaning steps)
##################################################################################
# begin cleaning pipe chain
###########################
linelist <- linelist_raw %>%
# standardize column name syntax
janitor::clean_names() %>%
# manually re-name columns
# NEW name # OLD name
rename(date_infection = infection_date,
date_hospitalisation = hosp_date,
date_outcome = date_of_outcome) %>%
# remove column
select(-c(row_num, merged_header, x25)) %>%
# fix incorrect values # old value # new value
mutate(date_onset = recode(date_onset, "15th April 2014" = "2014-04-15")) %>%
# correct the class of the columns
mutate(across(contains("date"), as.Date),
generation = as.numeric(generation),
age = as.numeric(age)) %>%
# create column: delay to hospitalisation
mutate(days_onset_hosp = as.numeric(date_hospitalisation - date_onset)) %>%
# ABOVE ARE UPSTREAM CLEANING STEPS ALREADY DISCUSSED
###################################################
# clean values of hospital column
mutate(hospital = recode(hospital,
# OLD = NEW
"Mitylira Hopital" = "Military Hospital",
"Mitylira Hospital" = "Military Hospital",
"Military Hopital" = "Military Hospital",
"Port Hopital" = "Port Hospital",
"Central Hopital" = "Central Hospital",
"other" = "Other",
"St. Marks Maternity Hopital (SMMH)" = "St. Mark's Maternity Hospital (SMMH)"
)) %>%
mutate(hospital = replace_na(hospital, "Missing")) %>%
# create age_years column (from age and age_unit)
mutate(age_years = case_when(
age_unit == "years" ~ age,
age_unit == "months" ~ age/12,
is.na(age_unit) ~ age,
TRUE ~ NA_real_))CAUTION: This tab may follow from previous tabs.
A typical early cleaning step is to filter the dataframe for specific rows using the dplyr verb filter(). Within filter(), give the logic that must be TRUE for a row in the dataset to be kept.
The tabs below show how to filter rows based on simple and complex logical conditions, and how to filter/subset rows as a stand-alone command and with base R
filter()filter()
This simple example re-defines the dataframe linelist as itself, having filtered the rows to meet a logical condition. Only the rows where the logical statement within the parentheses is TRUE are kept.
In this case, the logical statement is !is.na(case_id), which is asking whether the value in the column case_id is not missing (NA). Thus, rows where case_id is not missing are kept.
Before the filter is applied, the number of rows in linelist is 6609.
After the filter is applied, the number of rows in linelist is 6603.
filter()filter()
A more complex example using filter():
Below is a simple one-line command to create a histogram of onset dates. See that a second smaller outbreak from 2012-2013 is also included in this dataset. For our analyses, we want to remove entries from this earlier outbreak.
Can we just filter by date_onset to rows after June 2013? Caution! Applying the code filter(date_onset > as.Date("2013-06-01"))) would accidentally remove any rows in the later epidemic with a missing date of onset!
DANGER: Filtering to greater than (>) or less than (<) a date or number can remove any rows with missing values (NA)! This is because NA is treated as infinitely large and small.
Examine a cross-tabulation to make sure we exclude only the correct rows:
table(Hospital = linelist$hospital, # hospital name
YearOnset = lubridate::year(linelist$date_onset), # year of date_onset
useNA = "always") # show missing values
## YearOnset
## Hospital 2012 2013 2014 2015 <NA>
## Central Hospital 0 0 366 96 0
## Hospital A 225 65 0 0 0
## Hospital B 220 66 0 0 0
## Military Hospital 0 0 717 200 0
## Missing 0 0 1180 318 0
## Other 0 0 724 180 0
## Port Hospital 10 0 1462 345 0
## St. Mark's Maternity Hospital (SMMH) 0 0 336 93 0
## <NA> 0 0 0 0 0What other criteria can we filter on to remove the first outbreak from the dataset? We see that:
We want to exclude:
We start with a linelist of nrow(linelist). Here is our filter statement:
linelist <- linelist %>%
# keep rows where onset is after 1 June 2013 OR where onset is missing and it was a hospital OTHER than Hospital A or B
filter(date_onset > as.Date("2013-06-01") | (is.na(date_onset) & !hospital %in% c("Hospital A", "Hospital B")))
nrow(linelist)
## [1] 6017When we re-make the cross-tabulation, we see that Hospitals A & B are removed completely, and the 10 Port Hospital cases from 2012 & 2013 are removed, and all other values are the same - just as we wanted.
table(Hospital = linelist$hospital, # hospital name
YearOnset = lubridate::year(linelist$date_onset), # year of date_onset
useNA = "always") # show missing values
## YearOnset
## Hospital 2014 2015 <NA>
## Central Hospital 366 96 0
## Military Hospital 717 200 0
## Missing 1180 318 0
## Other 724 180 0
## Port Hospital 1462 345 0
## St. Mark's Maternity Hospital (SMMH) 336 93 0
## <NA> 0 0 0Multiple statements can be included within one filter command (separated by commas), or you can always pipe to a separate filter() command for clarity.
Note: some readers may notice that it would be easier to just filter by date_hospitalisation because it is 100% complete. This is true. But for pdate_onset is used for purposes of a complex filter example.
Filtering can also be done as a stand-alone command (not part of a pipe chain). Like other dplyr verbs, in this case the first argument must be the dataset itself.
# dataframe <- filter(dataframe, condition(s) for rows to keep)
linelist <- filter(linelist, !is.na(case_id))You can also use base R to subset using square brackets which reflect the [rows, columns] that you want to retain.
# dataframe <- dataframe[row conditions, column conditions] (blank means keep all)
linelist <- linelist[!is.na(case_id), ]TIP: Use bracket-subset syntax with View() to quickly review a few records.
This base R syntax can be handy when you want to quickly view a subset of rows and columns. Use the base R View() command (note the capital “V”) around the [] subset you want to see. The result will appear as a dataframe in your RStudio viewer panel. For example, if I want to review onset and hospitalization dates of 3 specific cases:
View the linelist in the viewer panel:
View specific data for three cases:
View(linelist[linelist$case_id %in% c("11f8ea", "76b97a", "47a5f5"), c("date_onset", "date_hospitalisation")])Note: the above command can also be written with dplyr verbs filter() and select() as below:
# CLEANING 'PIPE' CHAIN (starts with raw data and pipes it through cleaning steps)
##################################################################################
# begin cleaning pipe chain
###########################
linelist <- linelist_raw %>%
# standardize column name syntax
janitor::clean_names() %>%
# manually re-name columns
# NEW name # OLD name
rename(date_infection = infection_date,
date_hospitalisation = hosp_date,
date_outcome = date_of_outcome) %>%
# remove column
select(-c(row_num, merged_header, x25)) %>%
# fix incorrect values # old value # new value
mutate(date_onset = recode(date_onset, "15th April 2014" = "2014-04-15")) %>%
# correct the class of the columns
mutate(across(contains("date"), as.Date),
generation = as.numeric(generation),
age = as.numeric(age)) %>%
# create column: delay to hospitalisation
mutate(days_onset_hosp = as.numeric(date_hospitalisation - date_onset)) %>%
# clean values of hospital column
mutate(hospital = recode(hospital,
# OLD = NEW
"Mitylira Hopital" = "Military Hospital",
"Mitylira Hospital" = "Military Hospital",
"Military Hopital" = "Military Hospital",
"Port Hopital" = "Port Hospital",
"Central Hopital" = "Central Hospital",
"other" = "Other",
"St. Marks Maternity Hopital (SMMH)" = "St. Mark's Maternity Hospital (SMMH)"
)) %>%
mutate(hospital = replace_na(hospital, "Missing")) %>%
# create age_years column (from age and age_unit)
mutate(age_years = case_when(
age_unit == "years" ~ age,
age_unit == "months" ~ age/12,
is.na(age_unit) ~ age,
TRUE ~ NA_real_)) %>%
# ABOVE ARE UPSTREAM CLEANING STEPS ALREADY DISCUSSED
###################################################
filter(
# keep only rows where case_id is not missing
!is.na(case_id),
# also filter to keep only the second outbreak
date_onset > as.Date("2013-06-01") | (is.na(date_onset) & !hospital %in% c("Hospital A", "Hospital B")))CAUTION: This tab may follow from previous tabs.
Special approaches for creating numeric categories
Common examples include age categories, groups of lab values, etc.
There are several ways to create categories of a numeric column such as age. Here we will discuss:
age_categories(), from the epikit packagecut(), from base RSometimes, numeric variables will import as class “character”. This occurs if there are non-numeric characters in some of the values, for example an entry of “2 months” for age, or (depending on your R locale settings) if a comma is used in the decimals place (e.g. “4,5” to mean four and one half years).
For this example we will create an age_cat column using the age_years column.
age_categories()**age_categories()
With the epikit package, you can use the age_categories() function to easily categorize and label numeric columns (note: this can be applied to non-age numeric variables too). The output is an ordered factor.
The break values specified are included in the higher group, that is groups are open on the lower/left side. As shown below, you can add 1 to each break value to achieve groups that are open at the top/right.
Other optional arguments:
lower = Default is 0). The lowest number you want considered.upper = The highest number you want considered.by = The number of years between groups.separator = Default is “-”. Character between ages in labels.ceiling = Default FALSE. If TRUE, the highest break value is a ceiling and a category “XX+” is not included. Any values above highest break or upper (if defined) are categorized as NA.See the function’s Help page for more details (enter ?age_categories in the R console).
library(epikit)
# Simple example
################
linelist <- linelist %>%
mutate(age_cat = age_categories(age_years,
breakers = c(0, 5, 10, 15, 20, 30, 50, 70)))
# show table
table(linelist$age_cat, useNA = "always")
##
## 0-4 5-9 10-14 15-19 20-29 30-49 50-69 70+ <NA>
## 1078 1181 981 908 1089 665 24 0 91
# With ceiling set to TRUE
##########################
linelist <- linelist %>%
mutate(age_cat = age_categories(age_years,
breakers = c(0, 5, 10, 15, 20, 30, 50, 70),
upper = max(linelist$age_years, na.rm=T),
ceiling = TRUE)) # 70 is the ceiling
# show table
table(linelist$age_cat, useNA = "always")
##
## 0-4 5-9 10-14 15-19 20-29 30-49 50-70 <NA>
## 1078 1181 981 908 1089 665 24 91
# Include upper ends for the same categories
############################################
linelist <- linelist %>%
mutate(age_cat = age_categories(age_years,
upper = max(linelist$age_years, na.rm=T),
breakers = c(0, 6, 11, 16, 21, 31, 51, 71, 76)))
# show table
table(linelist$age_cat, useNA = "always")
##
## 0-5 6-10 11-15 16-20 21-30 31-50 51-70 71-75 76+ <NA>
## 1328 1149 977 818 1035 599 20 0 0 91cut()cut()
You can use the base R function cut(), which creates categories from a numeric variable. The differences from age_categories() are:
The basic syntax within cut() is to first provide the numeric variable to be cut (age_years), and then the breaks argument, which is a numeric vector (c()) of break points. Using cut(), the resulting column is an ordered factor.
If used within mutate() (a dplyr verb) it is not necessary to specify the dataframe before the column name (e.g. linelist$age_years).
cut() examplecut() example
Create new column of age categories (age_cat) by cutting the numeric age_year column at specified break points. The example below replicates the first age_categories() example.
c(0, 5, 10, 15, ...)cut() is that lower break values are excluded from each category, and upper break values are included. This is the opposite behavior from the age_categories() function.include.lowest = TRUElabels = argumentlinelist <- linelist %>%
mutate(age_cat = cut(age_years, # numeric column
breaks = c(0, 5, 10, 15, 20, 30, 50, 70, # break points...
max(linelist$age_years, na.rm=T)), # ... with dynamic last break as column max value
right = TRUE, # lower breaks included and upper excluded [a,b)
include.lowest = TRUE, # 0 included in lowest category
labels = c("0-4", "5-9", "10-14", "15-19", # manual labels - be careful!
"20-29", "30-49", "50-69", "70+")))
table(linelist$age_cat, useNA = "always")
##
## 0-4 5-9 10-14 15-19 20-29 30-49 50-69 70+ <NA>
## 1328 1149 977 818 1035 599 20 0 91cut() detailscut() details
Below is a detailed description of the behavior of using cut() to make the age_cat column. Key points:
The most simple command of cut() applied to age_years to make the new variable age_cat is below:
# Create new variable, by cutting the numeric age variable
# by default, upper break is excluded and lower break excluded from each category
linelist <- linelist %>%
mutate(age_cat = cut(age_years, breaks = c(0, 5, 10, 15, 20, 30, 50, 70, 100)))
# tabulate the number of observations per group
table(linelist$age_cat, useNA = "always")
##
## (0,5] (5,10] (10,15] (15,20] (20,30] (30,50] (50,70] (70,100]
## 1215 1149 977 818 1035 599 20 0
## <NA>
## 204By default, the categorization occurs so that the right/upper side is “open” and inclusive (and the left/lower side is “closed” or exclusive). The default labels use the notation “(A, B]”, which means the group does not include A (the lower break value), but includes B (the upper break value). Reverse this behavior by providing the right = TRUE argument.
Thus, by default “0” values are excluded from the lowest group, and categorized as NA. “0” values could be infants coded as age 0. To change this add the argument include.lowest = TRUE. Then, any “0” values are included in the lowest group. The automatically-generated label for the lowest category will change from “(0,B]” to “[0,B]”, which signifies that 0 values are included.
Check your work!!! Verify that each age value was assigned to the correct category by cross-tabulating the numeric and category columns. Examine assignment of boundary values (e.g. 15, if neighboring categories are 10-15 and 15-20).
# Cross tabulation of the numeric and category columns.
table("Numeric Values" = linelist$age_years, # names specified in table for clarity.
"Categories" = linelist$age_cat,
useNA = "always") # don't forget to examine NA values
## Categories
## Numeric Values (0,5] (5,10] (10,15] (15,20] (20,30] (30,50] (50,70]
## 0 0 0 0 0 0 0 0
## 0.0833333333333333 2 0 0 0 0 0 0
## 0.166666666666667 3 0 0 0 0 0 0
## 0.25 2 0 0 0 0 0 0
## 0.333333333333333 2 0 0 0 0 0 0
## 0.416666666666667 3 0 0 0 0 0 0
## 0.5 2 0 0 0 0 0 0
## 0.583333333333333 1 0 0 0 0 0 0
## 0.666666666666667 3 0 0 0 0 0 0
## 0.75 1 0 0 0 0 0 0
## 0.833333333333333 1 0 0 0 0 0 0
## 0.916666666666667 3 0 0 0 0 0 0
## 1 263 0 0 0 0 0 0
## 1.5 1 0 0 0 0 0 0
## 2 249 0 0 0 0 0 0
## 3 226 0 0 0 0 0 0
## 4 203 0 0 0 0 0 0
## 5 250 0 0 0 0 0 0
## 6 0 234 0 0 0 0 0
## 7 0 240 0 0 0 0 0
## 8 0 220 0 0 0 0 0
## 9 0 237 0 0 0 0 0
## 10 0 218 0 0 0 0 0
## 11 0 0 195 0 0 0 0
## 12 0 0 190 0 0 0 0
## 13 0 0 179 0 0 0 0
## 14 0 0 199 0 0 0 0
## 15 0 0 214 0 0 0 0
## 16 0 0 0 194 0 0 0
## 17 0 0 0 164 0 0 0
## 18 0 0 0 177 0 0 0
## 19 0 0 0 159 0 0 0
## 20 0 0 0 124 0 0 0
## 21 0 0 0 0 140 0 0
## 22 0 0 0 0 121 0 0
## 23 0 0 0 0 108 0 0
## 24 0 0 0 0 95 0 0
## 25 0 0 0 0 109 0 0
## 26 0 0 0 0 108 0 0
## 27 0 0 0 0 98 0 0
## 28 0 0 0 0 100 0 0
## 29 0 0 0 0 86 0 0
## 30 0 0 0 0 70 0 0
## 31 0 0 0 0 0 70 0
## 32 0 0 0 0 0 71 0
## 33 0 0 0 0 0 55 0
## 34 0 0 0 0 0 52 0
## 35 0 0 0 0 0 43 0
## 36 0 0 0 0 0 35 0
## 37 0 0 0 0 0 29 0
## 38 0 0 0 0 0 37 0
## 39 0 0 0 0 0 27 0
## 40 0 0 0 0 0 36 0
## 41 0 0 0 0 0 20 0
## 42 0 0 0 0 0 17 0
## 43 0 0 0 0 0 21 0
## 44 0 0 0 0 0 21 0
## 45 0 0 0 0 0 20 0
## 46 0 0 0 0 0 14 0
## 47 0 0 0 0 0 9 0
## 48 0 0 0 0 0 7 0
## 49 0 0 0 0 0 11 0
## 50 0 0 0 0 0 4 0
## 51 0 0 0 0 0 0 3
## 52 0 0 0 0 0 0 4
## 53 0 0 0 0 0 0 3
## 55 0 0 0 0 0 0 2
## 56 0 0 0 0 0 0 1
## 58 0 0 0 0 0 0 1
## 59 0 0 0 0 0 0 2
## 62 0 0 0 0 0 0 1
## 63 0 0 0 0 0 0 1
## 65 0 0 0 0 0 0 1
## 66 0 0 0 0 0 0 1
## <NA> 0 0 0 0 0 0 0
## Categories
## Numeric Values (70,100] <NA>
## 0 0 113
## 0.0833333333333333 0 0
## 0.166666666666667 0 0
## 0.25 0 0
## 0.333333333333333 0 0
## 0.416666666666667 0 0
## 0.5 0 0
## 0.583333333333333 0 0
## 0.666666666666667 0 0
## 0.75 0 0
## 0.833333333333333 0 0
## 0.916666666666667 0 0
## 1 0 0
## 1.5 0 0
## 2 0 0
## 3 0 0
## 4 0 0
## 5 0 0
## 6 0 0
## 7 0 0
## 8 0 0
## 9 0 0
## 10 0 0
## 11 0 0
## 12 0 0
## 13 0 0
## 14 0 0
## 15 0 0
## 16 0 0
## 17 0 0
## 18 0 0
## 19 0 0
## 20 0 0
## 21 0 0
## 22 0 0
## 23 0 0
## 24 0 0
## 25 0 0
## 26 0 0
## 27 0 0
## 28 0 0
## 29 0 0
## 30 0 0
## 31 0 0
## 32 0 0
## 33 0 0
## 34 0 0
## 35 0 0
## 36 0 0
## 37 0 0
## 38 0 0
## 39 0 0
## 40 0 0
## 41 0 0
## 42 0 0
## 43 0 0
## 44 0 0
## 45 0 0
## 46 0 0
## 47 0 0
## 48 0 0
## 49 0 0
## 50 0 0
## 51 0 0
## 52 0 0
## 53 0 0
## 55 0 0
## 56 0 0
## 58 0 0
## 59 0 0
## 62 0 0
## 63 0 0
## 65 0 0
## 66 0 0
## <NA> 0 91Read more about cut() in its Help page by entering ?cut in the R console.
Reversing break inclusion behavior in cut()
Lower break values will be included in each category (and upper break values excluded) if the argument right = is included and and set to TRUE. This is applied below - note how the values have shifted among the categories.
NOTE: If you include the include.lowest = TRUE argument and right = TRUE, the include.lowest will now apply to the highest break point value and category, not the lowest.
linelist <- linelist %>%
mutate(age_cat = cut(age_years,
breaks = c(0, 5, 10, 15, 20, 30, 50, 70, 100), # same breaks
right = FALSE, # include each *lower* break point
labels = c("0-4", "5-9", "10-14", "15-19",
"20-29", "30-49", "50-69", "70-100"))) # now the labels must change
table(linelist$age_cat, useNA = "always")
##
## 0-4 5-9 10-14 15-19 20-29 30-49 50-69 70-100 <NA>
## 1078 1181 981 908 1089 665 24 0 91Re-labeling NA values with cut()
Because cut() does not automatically label NA values, you may want to assign a label such as “Missing”. This requires a few extra steps because cut() automatically classified the new column age_cat as a Factor (a rigid column class with specific value labels).
First, convert age_cut from Factor to Character class, so you have flexibility to add new character values (e.g. “Missing”). Otherwise you will encounter an error. Then, use the dplyr verb replace_na() to replace NA values with a character value like “Missing”. These steps can be combined into one step, as shown below.
Note that Missing has been added, but the order of the categories is now wrong (alphabetical).
linelist <- linelist %>%
# cut() creates age_cat, automatically of class Factor
mutate(age_cat = cut(age_years,
breaks = c(0, 5, 10, 15, 20, 30, 50, 70, 100),
right = FALSE,
labels = c("0-4", "5-9", "10-14", "15-19",
"20-29", "30-49", "50-69", "70-100")),
# convert to class Character, and replace NA with "Missing"
age_cat = replace_na(as.character(age_cat), "Missing"))
table(linelist$age_cat, useNA = "always")
##
## 0-4 10-14 15-19 20-29 30-49 5-9 50-69 Missing <NA>
## 1078 981 908 1089 665 1181 24 91 0To fix this, re-convert age_cat to a factor, and define the order of the levels correctly.
linelist <- linelist %>%
# cut() creates age_cat, automatically of class Factor
mutate(age_cat = cut(age_years,
breaks = c(0, 5, 10, 15, 20, 30, 50, 70, 100),
right = FALSE,
labels = c("0-4", "5-9", "10-14", "15-19",
"20-29", "30-49", "50-69", "70-100")),
# convert to class Character, and replace NA with "Missing"
age_cat = replace_na(as.character(age_cat), "Missing"),
# re-classify age_cat as Factor, with correct level order and new "Missing" level
age_cat = factor(age_cat, levels = c("0-4", "5-9", "10-14", "15-19", "20-29",
"30-49", "50-69", "70-100", "Missing")))
table(linelist$age_cat, useNA = "always")
##
## 0-4 5-9 10-14 15-19 20-29 30-49 50-69 70-100 Missing <NA>
## 1078 1181 981 908 1089 665 24 0 91 0If you want a fast way to make breaks and labels, you can use something like below (adjust to your specific situation). See the page on using seq() and rep() and c() TO DO
# Make break points from 0 to 90 by 5
age_seq = seq(from = 0, to = 90, by = 5)
age_seq
# Make labels for the above categories, assuming default cut() settings
age_labels = paste0(age_seq+1, "-", age_seq + 5)
age_labels
# check that both vectors are the same length
length(age_seq) == length(age_labels)
# # Use them in the cut() command
# cut(linelist$age, breaks = age_seq, labels = age_labels)case_when()case_when()
The dplyr function case_when() can also be used to create numeric categories.
NA values in one stepIf using case_when() please review the in-depth page on it, as the logic and order of assignment are important understand to avoid errors.
CAUTION: In case_when() all right-hand side values must be of the same class. Thus, if your categories are character values (e.g. “20-30 years”) then any designated outcome for NA age values must also be character (“Missing”, or the special NA_character_ instead of NA).
You will need to designate the column as a factor (by wrapping case_when() in the function factor()) and provide the ordering of the factor levels using the levels = argument after the close of the case_when() function. When using cut(), the factor and ordering of levels is done automatically.
linelist <- linelist %>%
mutate(age_cat = factor(case_when(
# provide the case_when logic and outcomes
age_years >= 0 & age_years < 5 ~ "0-4", # logic by age_year value
age_years >= 5 & age_years < 10 ~ "5-9",
age_years >= 10 & age_years < 15 ~ "10-14",
age_years >= 15 & age_years < 20 ~ "15-19",
age_years >= 20 & age_years < 30 ~ "20-29",
age_years >= 30 & age_years < 50 ~ "30-49",
age_years >= 50 & age_years < 70 ~ "50-69",
age_years >= 45 & age_years <= 100 ~ "70-100",
is.na(age_years) ~ "Missing", # if age_years is missing
TRUE ~ "Check value" # catch-all alarm to trigger review
), levels = c("0-4","5-9", "10-14", "15-19", "20-29", "30-49", "50-69", "70-100", "Missing", "Check value"))
)
table(linelist$age_cat, useNA = "always")
##
## 0-4 5-9 10-14 15-19 20-29 30-49
## 1078 1181 981 908 1089 665
## 50-69 70-100 Missing Check value <NA>
## 24 0 91 0 0Below, code to create two categorical age columns is added to the cleaning pipe chain:
# CLEANING 'PIPE' CHAIN (starts with raw data and pipes it through cleaning steps)
##################################################################################
# begin cleaning pipe chain
###########################
linelist <- linelist_raw %>%
# standardize column name syntax
janitor::clean_names() %>%
# manually re-name columns
# NEW name # OLD name
rename(date_infection = infection_date,
date_hospitalisation = hosp_date,
date_outcome = date_of_outcome) %>%
# remove column
select(-c(row_num, merged_header, x25)) %>%
# fix incorrect values # old value # new value
mutate(date_onset = recode(date_onset, "15th April 2014" = "2014-04-15")) %>%
# correct the class of the columns
mutate(across(contains("date"), as.Date),
generation = as.numeric(generation),
age = as.numeric(age)) %>%
# create column: delay to hospitalisation
mutate(days_onset_hosp = as.numeric(date_hospitalisation - date_onset)) %>%
# clean values of hospital column
mutate(hospital = recode(hospital,
# OLD = NEW
"Mitylira Hopital" = "Military Hospital",
"Mitylira Hospital" = "Military Hospital",
"Military Hopital" = "Military Hospital",
"Port Hopital" = "Port Hospital",
"Central Hopital" = "Central Hospital",
"other" = "Other",
"St. Marks Maternity Hopital (SMMH)" = "St. Mark's Maternity Hospital (SMMH)"
)) %>%
mutate(hospital = replace_na(hospital, "Missing")) %>%
# create age_years column (from age and age_unit)
mutate(age_years = case_when(
age_unit == "years" ~ age,
age_unit == "months" ~ age/12,
is.na(age_unit) ~ age,
TRUE ~ NA_real_)) %>%
filter(
# keep only rows where case_id is not missing
!is.na(case_id),
# also filter to keep only the second outbreak
date_onset > as.Date("2013-06-01") | (is.na(date_onset) & !hospital %in% c("Hospital A", "Hospital B"))) %>%
# ABOVE ARE UPSTREAM CLEANING STEPS ALREADY DISCUSSED
###################################################
mutate(
# age categories: custom
age_cat = epikit::age_categories(age_years, breakers = c(0, 5, 10, 15, 20, 30, 50, 70)),
# age categories: 0 to 85 by 5s
age_cat5 = epikit::age_categories(age_years, breakers = seq(0, 85, 5)))
https://cran.r-project.org/web/packages/dplyr/vignettes/rowwise.html
CAUTION: This tab may follow from previous tabs.
A transformation can be applied to multiple variables at once using the across() function from the package dplyr (contained within tidyverse package).
across() can be used with any dplyr verb, but commonly with as mutate(), filter(), or summarise(). Here are some examples to get started.
Example of how one would change all columns to character class
#to change all columns to character class
linelist <- linelist %>%
mutate(across(everything(), as.character))Change only numeric columns
Here are a few online resources on using across(): Hadley Wickham’s thoughts/rationale
CAUTION: This tab may follow from previous tabs.
The package dplyr offers the distinct() function to reduce the dataframe to only unique rows - removing duplicates.
In this case we just want to remove rows that are complete duplicates, so we just add the simple command distinct().
For more complex deduplications see the page on deduplicating.
We begin with 6017 rows in linelist.
After deduplication there are 5888 rows.
Below, the distinct() command is added to the cleaning pipe chain:
# CLEANING 'PIPE' CHAIN (starts with raw data and pipes it through cleaning steps)
##################################################################################
# begin cleaning pipe chain
###########################
linelist <- linelist_raw %>%
# standardize column name syntax
janitor::clean_names() %>%
# manually re-name columns
# NEW name # OLD name
rename(date_infection = infection_date,
date_hospitalisation = hosp_date,
date_outcome = date_of_outcome) %>%
# remove column
select(-c(row_num, merged_header, x25)) %>%
# fix incorrect values # old value # new value
mutate(date_onset = recode(date_onset, "15th April 2014" = "2014-04-15")) %>%
# correct the class of the columns
mutate(across(contains("date"), as.Date),
generation = as.numeric(generation),
age = as.numeric(age)) %>%
# create column: delay to hospitalisation
mutate(days_onset_hosp = as.numeric(date_hospitalisation - date_onset)) %>%
# clean values of hospital column
mutate(hospital = recode(hospital,
# OLD = NEW
"Mitylira Hopital" = "Military Hospital",
"Mitylira Hospital" = "Military Hospital",
"Military Hopital" = "Military Hospital",
"Port Hopital" = "Port Hospital",
"Central Hopital" = "Central Hospital",
"other" = "Other",
"St. Marks Maternity Hopital (SMMH)" = "St. Mark's Maternity Hospital (SMMH)"
)) %>%
mutate(hospital = replace_na(hospital, "Missing")) %>%
# create age_years column (from age and age_unit)
mutate(age_years = case_when(
age_unit == "years" ~ age,
age_unit == "months" ~ age/12,
is.na(age_unit) ~ age,
TRUE ~ NA_real_)) %>%
filter(
# keep only rows where case_id is not missing
!is.na(case_id),
# also filter to keep only the second outbreak
date_onset > as.Date("2013-06-01") | (is.na(date_onset) & !hospital %in% c("Hospital A", "Hospital B"))) %>%
mutate(
# age categories: custom
age_cat = epikit::age_categories(age_years, breakers = c(0, 5, 10, 15, 20, 30, 50, 70)),
# age categories: 0 to 85 by 5s
age_cat5 = epikit::age_categories(age_years, breakers = seq(0, 85, 5))) %>%
distinct()This page will cover:
NA is displayed in plotsThe following are useful functions when assessing or handling missing values:
is.na() and !is.na()
To identify missing values use is.na() or its opposite (with ! in front). Both are from base R.
These return a logical vector (TRUE or FALSE). Remember that you can sum() the resulting vector to count the number TRUE, e.g. sum(is.na(linelist$date_outcome)).
my_vector <- c(1, 4, 56, NA, 5, NA, 22)
is.na(my_vector)
## [1] FALSE FALSE FALSE TRUE FALSE TRUE FALSE
!is.na(my_vector)
## [1] TRUE TRUE TRUE FALSE TRUE FALSE TRUEna.omit()
This function, if applied to a dataframe, will remove rows with any missing values. It is also from base R.
If applied to a vector, it will remove NA values from the vector it is applied to. For example:
na.rm = TRUE
Often a mathematical function will by default include NA in calculations, which results in the function returning NA (this is designed intentionally, to make you aware that you have missing data).
You can usually avoid this by removing missing values from the calculation, by including the argument na.rm = TRUE (na.rm stands for “remove NA”).
Change in percent of weekly observations that are missing in X column.
outcome_missing <- linelist %>%
mutate(week = lubridate::floor_date(date_onset, "week")) %>%
group_by(week) %>%
summarize(n_obs = n(),
outcome_missing = sum(is.na(outcome) | outcome == ""), # include "" because this is character
outcome_p_miss = outcome_missing / n_obs) %>%
reshape2::melt(id.vars = c("week")) %>%
filter(grepl("_p_", variable))Then we plot the proportion missing as a line, by week
ggplot(data = outcome_missing)+
geom_line(aes(x = week, y = value, group = variable, color = variable), size = 2, stat = "identity")+
labs(title = "Weekly missingness in 'Outcome'",
x = "Week",
y = "Proportion missing") +
scale_color_discrete(name = "", labels = c("Weekly proportion of missing outcomes"))+
scale_y_continuous(breaks = c(seq(0,1,0.1)))+
theme_minimal()+
theme(
legend.position = "bottom"
)NA in plotsNA in plots
(pivoting/melting etc.) Transforming datasets from wide-to-long, or long-to-wide…
https://datacarpentry.org/r-socialsci/03-dplyr-tidyr/index.html
Transforming a dataset from wide to long
We start with data that is in a wide format, e.g. our linelist.
pivot_longer()dplyr pivot_wider()
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
https://datacarpentry.org/R-genomics/04-dplyr.html#mutate
Keep the title of this section as “Overview”.
This tab should include:
Tidyverse - grouping by values
.drop=F in group_by() command
Keep the title of this section as “Preparation”.
Data preparation steps such as:
group_by()aggregate()This tab should stay with the name “Resources”. Links to other online tutorials or resources.
This page covers the following subjects:
Load packages
pacman::p_load(tidyverse, # deduplication, grouping, and slicing functions
janitor, # function for reviewing duplicates
stringr # for string searches, can be used in "rolling-up" values
) Example dataset
For demonstration, we will use the fake dataset below. It is a record of COVID-19 phone encounters, including with contacts and with cases.
recordID (computer glitch)recordIDThis tab uses the dataset from the Preparation tab to describe how to review and remove duplicate rows in a dataframe. It also show how to handle duplicate elements in a vector.
To quickly review rows that have duplicates, you can use get_dupes() from the janitor package. By default, all columns are considered when duplicates are evaluated - rows returned are 100% duplicates considering the values in all columns.
In the obs dataframe, the first two rows that are 100% duplicates - they have the same value in every column (including the recordID column, which is supposed to be unique - it must be some computer glitch). The returned dataframe automatically includes a new column dupe_count, showing the number of rows with that combination of duplicate values.
However, if we choose to ignore recordID, the 3rd and 4th rows rows are also duplicates. That is, they have the same values in all columns except for recordID. You can specify specific columns to be ignored in the function using a - minus symbol.
# Duplicates when column recordID is not considered
obs %>%
janitor::get_dupes(-recordID) # if multiple columns, wrap them in c()You can also positively specify the columns to consider. Below, only rows that have the same values in the name and purpose columns are returned. Notice how “amrish” now has dupe_count equal to 3 to reflect his three “contact” encounters.
*Scroll left for more rows**
See ?get_dupes for more details, or see this online reference
To keep only unique rows of a dataframe, use distinct() from dplyr. Rows that are duplicates are removed such that only the first of such rows is kept. By default, “first” means the highest rownumber (order of rows top-to-bottom). Only unique rows are kept. In the example below, one duplicate row (the first row, for “adam”) has been removed (n is now 18, not 19 rows).
Scroll to the left to see the entire dataframe
# added to a chain of pipes (e.g. data cleaning)
obs %>%
distinct(across(-recordID), # reduces dataframe to only unique rows (keeps first one of any duplicates)
.keep_all = TRUE)
# if outside pipes, include the data as first argument
# distinct(obs)CAUTION: If using distinct() on grouped data, the function will apply to each group.
Deduplicate based on specific columns
You can also specify columns to be the basis for de-duplication. In this way, the de-duplication only applies to rows that are duplicates within the specified columns. Unless specified with .keep_all = TRUE, all columns not mentioned will be dropped.
In the example below, the de-duplication only applies to rows that have identical values for name and purpose columns. Thus, “brian” has only 2 rows instead of 3 - his first “contact” encounter and his only “case” encounter. To adjust so that brian’s latest encounter of each purpose is kept, see the tab on Slicing within groups.
Scroll to the left to see the entire dataframe
The function duplicated() from base R will evaluate a vector (column) and return a logical vector of the same length (TRUE/FALSE). The first time a value appears, it will return FALSE (not a duplicate), and subsequent times that value appears it will return TRUE. Note how NA is treated the same as any other value.
x <- c(1, 1, 2, NA, NA, 4, 5, 4, 4, 1, 2)
duplicated(x)
## [1] FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUETo return only the duplicated elements, you can use brackets to subset the original vector:
To return only the unique elements, use unique() from base R. To remove NAs from the output, nest na.omit() within unique().
To return duplicate rows
In base R, you can also see which rows are 100% duplicates in a dataframe df with the command duplicated(df) (returns a logical vector of the rows).
Thus, you can also use the base subset [ ] on the dataframe to see the duplicated rows with df[duplicated(df),] (don’t forget the comma, meaning that you want to see all columns!).
To return unique rows
See the notes above. To see the unique rows you add the logical negator ! in front of the duplicated() function:
df[!duplicated(df),]
To return rows that are duplicates of only certain columns
Subset the df that is within the duplicated() parentheses, so this function will operate on only certain columns of the df.
To specify the columns, provide column numbers or names after a comma (remember, all this is within the duplicated() function).
Be sure to keep the comma , outside after the duplicated() function as well!
For example, to evaluate only columns 2 through 5 for duplicates: df[!duplicated(df[, 2:5]),]
To evaluate only columns name and purpose for duplicates: df[!duplicated(df[, c("name", "purpose)]),]
To “slice” a dataframe is useful in de-duplication if you have multiple rows per functional group (e.g. per “person”) and you only want to analyze one or some of them. Think of slicing a filter on the rows, by row number/position.
The basic slice() function accepts a number n. If positive, only the nth row is returned. If negative, all rows except the nth are returned.
Variations include:
slice_min() and slice_max() - to keep only the row with the minimium or maximum value of the specified column. Also worked with ordered factors.slice_head() and slice_tail - to keep only the first or last rowslice_sample() - to keep only a random sample of the rowsUse arguments n = or prop = to specify the number or proportion of rows to keep. If not using the function in a pipe chain, provide the data argument first (e.g. slice(df, n = 2)). See ?slice for more information.
Other arguments:
.order_by = - used in slice_min() and slice_max() this is a column to order by before slicing.
with_ties = - TRUE by default, meaning ties are kept.
.preserve = - FALSE by default. If TRUE then the grouping structure is re-calculated after slicing.
weight_by = - Optional, numeric column to weight by (bigger number more likely to get sampled). Also replace = for whether sampling is done with/without replacement.
TIP: When using slice_max() and slice_min(), be sure to specify/write the n = (e.g. n = 2, not just 2). Otherwise you may get an error Error:…is not empty.
NOTE: You may encounter the function top_n(), which has been superseded by the slice functions.
Here, the basic slice() function is used to keep only the 4th row:
The slice_*() functions can be very useful if applied to a grouped dataframe, as the slice operation is performed on each group separately. Use the function group_by() in conjunction with slice() to group the data and then take a slice from each group.
This is helpful for de-duplication if you have multiple rows per person but only want to keep one of them. You first use group_by() with key columns that are the same, and then use a slice function on a column that will differ among the grouped rows.
In the example below, to keep only the latest encounter per person, we group the rows by name and then use slice_max() with n = 1 on the date column. Be aware! To apply a function like slice_max() on dates, the date column must be class Date.
By default, “ties” (e.g. same date in this scenario) are kept, and we would still get multiple rows for some people (e.g. adam). To avoid this we set with_ties = FALSE. We get back only one row per person.
CAUTION: If using arrange(), specify .by_group = TRUE to have the data arranged within each group.
DANGER: If with_ties = FALSE, the first row of a tie is kept. This may be deceptive. See how for Mariah, she has two encounters on her latest date (6 Jan) and the first (earliest) one was kept. Likely, we want to keep her later encounter on that day. See how to “break” these ties in the next example.
obs %>%
group_by(name) %>% # group the rows by 'name'
slice_max(date, # keep row per group with maximum date value
n = 1, # keep only the single highest row
with_ties = F) # if there's a tie (of date), take the first rowBreaking “ties”
Multiple slice statements can be run to “break ties”. In this case, if a person has multiple encounters on their latest date, the encounter with the latest time is kept (lubridate::hm() is used to convert the character times to a sortable time class).
Note how now, the one row kept for “Mariah” on 6 Jan is encounter 3 from 08:32, not encounter 2 at 07:25.
# Example of multiple slice statements to "break ties"
obs %>%
group_by(name) %>%
# FIRST - slice by latest date
slice_max(date, n = 1, with_ties = TRUE) %>%
# SECOND - if there is a tie, select row with latest time; ties prohibited
slice_max(lubridate::hm(time), n = 1, with_ties = FALSE)In the example above, it would also have been possible to slice by encounter number, but we showed the slice on date and time for example purposes.
TIP: To use slice_max() or slice_min() on a “character” column, mutate it to an ordered factor class!
If you want to keep all records but mark only some for analysis, consider a two-step approach utilizing a unique recordID/encounter number:
case_when(), based on whether their record unique identifier (recordID in this example) is present in the reduced dataframe.# 1. Define dataframe of rows to keep for analysis
obs_keep <- obs %>%
group_by(name) %>%
slice_max(encounter, n = 1, with_ties = FALSE) # keep only latest encounter per person
# 2. Mark original dataframe
obs_marked <- obs %>%
# make new dup_record column
mutate(dup_record = case_when(
# if record is in obs_keep dataframe
recordID %in% obs_keep$recordID ~ "For analysis",
# all else marked as "Ignore" for analysis purposes
TRUE ~ "Ignore"))
# print
obs_markedCreate a column that contains a metric for the row’s completeness (non-missingness). This could be helpful when deciding which rows to prioritize over others when de-duplicating/slicing.
In this example, “key” columns over which you want to measure completeness are saved in a vector of column names.
Then the new column key_completeness is created with mutate(). The new value in each row is defined as a calculated fraction: the number of non-missing values in that row among the key columns, divided by the number of key columns.
This involves the function rowSums() from base R. Also used is ., which within piping refers to the dataframe at that point in the pipe (in this case, it is being subset with brackets []).
*Scroll to the right to see more rows**
This tab describes:
This tab uses the example dataset from the Preparation tab.
The code example below uses group_by() and summarise() to group rows by person, and then paste together all unique values within the grouped rows. Thus, you get one summary row per person. A few notes:
na.omit() with unique()na.omit() removes NA values, but if this is not desired it can be removed paste0(.x)…Scroll to the left to see more rows
# "Roll-up" values into one row per group (per "personID")
cases_rolled <- obs %>%
# create groups by name
group_by(personID) %>%
# order the rows within each group (e.g. by date)
arrange(date, .by_group = TRUE) %>%
# For each column, paste together all values within the grouped rows, separated by ";"
summarise(
across(everything(), # apply to all columns
~paste0(na.omit(.x), collapse = "; "))) # function is defined which combines non-NA valuesThe result is one row per group (ID), with entries arranged by date and pasted together.
This variation shows unique values only:
# Variation - show unique values only
cases_rolled <- obs %>%
group_by(personID) %>%
arrange(date, .by_group = TRUE) %>%
summarise(
across(everything(), # apply to all columns
~paste0(unique(na.omit(.x)), collapse = "; "))) # function is defined which combines unique non-NA valuesThis variation appends a suffix to each column.
In this case "_roll" to signify that it has been rolled:
If you then want to evaluate all of the rolled values, and keep only a specific value (e.g. “best” or “maximum” value), you can use mutate() across the desired columns, to implement case_when(), which uses str_detect() from the stringr package to sequentially look for string patterns and overwrite the cell content.
# CLEAN CASES
#############
cases_clean <- cases_rolled %>%
# clean Yes-No-Unknown vars: replace text with "highest" value present in the string
mutate(across(c(contains("symptoms_ever")), # operates on specified columns (Y/N/U)
list(mod = ~case_when( # adds suffix "_mod" to new cols; implements case_when()
str_detect(.x, "Yes") ~ "Yes", # if "Yes" is detected, then cell value converts to yes
str_detect(.x, "No") ~ "No", # then, if "No" is detected, then cell value converts to no
str_detect(.x, "Unknown") ~ "Unknown", # then, if "Unknown" is detected, then cell value converts to Unknown
TRUE ~ as.character(.x)))), # then, if anything else if it kept as is
.keep = "unused") # old columns removed, leaving only _mod columnsNow you can see in the column symptoms_ever that if the person EVER said “Yes” to symptoms, then only “Yes” is displayed.
Much of the information in this page is adapted from these resources and vignettes online:
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
antijoins as well
Sub-tabs if necessary. Re-name as needed.
rowmatcher other options (finlay?)
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
This tab demonstrates use of the stringr package to evaluate and manage character (strings).
str_length(), str_sub(), word()str_c(), str_glue(), str_order()str_sub(), str_replace_all()str_pad(), str_trunc(), str_wrap()str_to_upper(), str_to_title(), str_to_lower(), str_to_sentence()str_detect(), str_subset(), str_match()For ease of display most examples are shown acting on a short defined character vector, however they can easily be applied/adapted to a column within a dataset.
Much of this page is adapted from this online vignette
Install or load the stringr package.
# install or load the stringr package
pacman::p_load(stringr, # many functions for handling strings
tidyverse, # for optional data manipulation
tools # alternative for converting to title case
)A reference sheet for stringr functions can be found here
Evaluate the length of a string
Alternatively, use nchar() from base R
Subset/extract string by position
Use str_sub() to return only a part of a string. The function takes three main arguments:
A few notes on position numbers:
Below are some examples applied to the string “pneumonia”:
# third from left
str_sub("pneumonia", 3, 3)
## [1] "e"
# 0 is not present
str_sub("pneumonia", 0, 0)
## [1] ""
# 6th from right, to the first from right
str_sub("pneumonia", 6, -1)
## [1] "onia"
# fifth from right, to the first from right
str_sub("pneumonia", -5, -1)
## [1] "monia"
# positions outside the string
str_sub("pneumonia", 4, 15)
## [1] "umonia"Subset string by word position
To extract the nth ‘word’, use word(), also from stringr. Provide the string(s), then the first word position to extract, and the last word position to extract.
By default, the separator between ‘words’ is assumed to be a space, unless otherwise indicated with sep = (e.g. sep = "_" when words are separated by underscores.
# strings to evaluate
chief_complaints <- c("I just got out of the hospital 2 days ago, but still can barely breathe.",
"My stomach hurts",
"Severe ear pain")
# extract 1st-3rd words of each string
word(chief_complaints, start = 1, end = 3, sep = " ")
## [1] "I just got" "My stomach hurts" "Severe ear pain"This section covers using str_c(), str_glue(), str_order(), to combine, arrange, and paste together strings.
It is common to see base R functions paste() and paste0(), which concatenate vectors after converting all parts to character. The act similarly to str_c() but the syntax differs - the parts (either text or code/pre-defined objects) are separated by commas, for example: paste("Regional hospital needs", n_beds, "beds and", n_masks, "masks."). The sep and collapse arguments can be adjusted. By default sep is a space, unless using paste0() where there is no space between parts.
To combine multiple strings into one string, you can use str_c(), which is the stringr version of c() (concatenate).
The argument sep = inserts characters between each input vectors (e.g. a comma or newline "\n")
The argument collapse = is relevant if producing multiple elements. The example below shows the combination of first and last names. The sep value goes between each first and last name, while the collapse value goes between the people.
first_names <- c("abdul", "fahruk", "janice")
last_names <- c("hussein", "akinleye", "musa")
# sep is between the respective strings, while collapse is between the elements produced
str_c(first_names, last_names, sep = " ", collapse = "; ")
## [1] "abdul hussein; fahruk akinleye; janice musa"
# For newlines to print correctly, the phrase may need to be wrapped in cat()
cat(str_c(first_names, last_names, sep = " ", collapse = ";\n"))
## abdul hussein;
## fahruk akinleye;
## janice musastr_glue()
You can also combine strings and other pre-defined values and characters with str_glue(). This is a very useful function for creating dynamic plot captions, as demonstrated below.
{}. There can be many curly brackets.\n), use format() to display dates, use Sys.Date() to display the current date.%>% pipe operator, ensure the tidyverse package is loaded.A simple example:
str_glue("The linelist is current to {format(Sys.Date(), '%d %b %Y')} and includes {nrow(linelist)} cases.")
## The linelist is current to 23 Jan 2021 and includes 5888 cases.An alternative format is to use placeholders within the brackets and define the code in separate arguments at the end of the str_glue() function, as below. This can improve code readability if the codes are long.
str_glue("Data source is the confirmed case linelist as of {current_date}.\nThe last case was reported hospitalized on {last_hospital}.\n{n_missing_onset} cases are missing date of onset and not shown",
current_date = format(Sys.Date(), '%d %b %Y'),
last_hospital = format(as.Date(max(linelist$date_hospitalisation, na.rm=T)), '%d %b %Y'),
n_missing_onset = nrow(linelist %>% filter(is.na(date_onset)))
)
## Data source is the confirmed case linelist as of 23 Jan 2021.
## The last case was reported hospitalized on 30 Apr 2015.
## 0 cases are missing date of onset and not shownSometimes, it is useful to pull data from dataframe and have it pasted together in sequence. Below is an example using this dataset to make a summary output of jurisdictions and the new and total cases:
DT::datatable(case_table, rownames = FALSE, filter="top", options = list(pageLength = 5, scrollX=T) )Use str_c() with the dataframe and column names (as in the example above with first & last names). Provide sep and collapse arguments.
str_c(case_table$zone, case_table$new_cases, sep = " = ", collapse = "; ")
## [1] "Zone 1 = 3; Zone 2 = 0; Zone 3 = 7; Zone 4 = 0; Zone 5 = 15"We add the text “New Cases:” to the beginning of the summary by using wrapping with a separate str_c(). If “New Cases” was added within the original str_c(), it would appear multiple times.
str_c("New Cases: ", str_c(case_table$zone, case_table$new_cases, sep = " = ", collapse = "; "))
## [1] "New Cases: Zone 1 = 3; Zone 2 = 0; Zone 3 = 7; Zone 4 = 0; Zone 5 = 15"You can achieve a similar result with str_glue(), with newlines added automatically:
str_glue("{case_table$zone}: {case_table$new_cases} new cases ({case_table$total_cases} total cases)")
## Zone 1: 3 new cases (40 total cases)
## Zone 2: 0 new cases (4 total cases)
## Zone 3: 7 new cases (25 total cases)
## Zone 4: 0 new cases (10 total cases)
## Zone 5: 15 new cases (103 total cases)To use str_glue() but have more control (e.g. to use double newlines), wrap it within str_c() and adjust the collapse value. You may need to print using cat() to correctly print the newlines.
case_summary <- str_c(str_glue("{case_table$zone}: {case_table$new_cases} new cases ({case_table$total_cases} total cases)"), collapse = "\n\n")
cat(case_summary) # print
## Zone 1: 3 new cases (40 total cases)
##
## Zone 2: 0 new cases (4 total cases)
##
## Zone 3: 7 new cases (25 total cases)
##
## Zone 4: 0 new cases (10 total cases)
##
## Zone 5: 15 new cases (103 total cases)Several strings can be sorted by alphabetical order. str_order() returns the order, while str_sort() returns the strings in that order.
# strings
health_zones <- c("Alba", "Takota", "Delta")
# return the alphabetical order
str_order(health_zones)
## [1] 1 3 2
# return the strings in alphabetical order
str_sort(health_zones)
## [1] "Alba" "Delta" "Takota"To use a different alphabet, add the argument locale =. See the full list of locales by entering stringi::stri_locale_list() in the R console.
It is common to see base R functions paste() and paste0(), which concatenate vectors after converting all parts to character. The act similarly to str_c() but the syntax differs - the parts (either text or code/pre-defined objects) are separated by commas, for example: paste("Regional hospital needs", n_beds, "beds and", n_masks, "masks."). The sep and collapse arguments can be adjusted. By default sep is a space, unless using paste0() where there is no space between parts.
Replace specific character positions
str_sub() paired with the assignment operator (<-) can be used to modify a part of a string:
word <- "pneumonia"
# convert the third and fourth characters to X
str_sub(word, 3, 4) <- "XX"
word
## [1] "pnXXmonia"An example applied to multiple strings (e.g. a column). Note the expansion in length of “HIV”.
words <- c("pneumonia", "tubercolosis", "HIV")
# convert the third and fourth characters to X
str_sub(words, 3, 4) <- "XX"
words
## [1] "pnXXmonia" "tuXXrcolosis" "HIXX"Replace patterns
Use str_replace_all() as a “find and replace” tool. First, provide the strings to be evaluated, then the pattern to be replaced, and then the replacement value. The example below replaces all instances of “dead” with “deceased”. Note, this IS case sensitive.
outcome <- c("Karl: dead",
"Samantha: dead",
"Marco: not dead")
str_replace_all(outcome, "dead", "deceased")
## [1] "Karl: deceased" "Samantha: deceased" "Marco: not deceased"To replace a pattern with NA, use str_replace_na(). The function str_replace() replaces only the first instance of the pattern within each evaluated string.
Increase minimum length (pad)
Use str_pad() to add characters to a string, to a minimum length.
By default spaces are added, but you can also pad with other characters using the pad = argument.
# ICD codes of differing length
ICD_codes <- c("R10.13",
"R10.819",
"R17")
# ICD codes padded to 7 characters on the right side
str_pad(ICD_codes, 7, "right")
## [1] "R10.13 " "R10.819" "R17 "
# Pad with periods instead of spaces
str_pad(ICD_codes, 7, "right", pad = ".")
## [1] "R10.13." "R10.819" "R17...."For example, to pad numbers with leading zeros (such as for hours or minutes), you can pad the number to minimum length of 2 with pad = "0".
# Add leading zeros to two digits (e.g. for times minutes/hours)
str_pad("4", 2, pad = "0")
## [1] "04"
# example using a numeric column named "hours"
# hours <- str_pad(hours, 2, pad = "0")Truncate/shorten
str_trunc() sets a maximum length for each string. If a string exceeds this length, it is truncated (shortened) and an ellipsis (…) is included to indicate that the string was previously longer. Note that the ellipsis is counted in the length. The ellipsis characters can be changed with the argument ellipsis =. The optional side = argument specifies which where the ellipsis will appear within the truncated string (“left”, “right”, or “center”).
original <- "Symptom onset on 4/3/2020 with vomiting"
str_trunc(original, 10, "center")
## [1] "Symp...ing"To ensure each value is the same length
Use str_trunc() to set a maximum length, and then use str_pad() to expand the very short strings to that truncated length. In the example below, 6 is set as the maximum length (one value is truncated), and then a very short value is padded to achieve length of 6.
# ICD codes of differing length
ICD_codes <- c("R10.13",
"R10.819",
"R17")
# truncate to maximum length of 6
ICD_codes_2 <- str_trunc(ICD_codes, 6)
ICD_codes_2
## [1] "R10.13" "R10..." "R17"
# expand to minimum length of 6
ICD_codes_3 <- str_pad(ICD_codes_2, 6, "right")
ICD_codes_3
## [1] "R10.13" "R10..." "R17 "Remove leading/trailing whitespace
Use str_trim() to remove spaces, newlines (\n) or tabs (\t) on sides of a string input.
Add "right" "left", or "both" to the command to specify which side to trim (e.g. str_trim(x, "right").
# ID numbers with excess spaces on right
IDs <- c("provA_1852 ", # two excess spaces
"provA_2345", # zero excess spaces
"provA_9460 ") # one excess space
# IDs trimmed to remove excess spaces on right side only
str_trim(IDs)
## [1] "provA_1852" "provA_2345" "provA_9460"Remove repeated whitespace within strings
Use str_squish() to remove repeated spaces that appear inside a string. For example, to convert double spaces into single spaces. It also removes spaces, newlines, or tabs on the outside of the string like str_trim().
# original contains excess spaces within string
str_squish(" Pt requires IV saline\n")
## [1] "Pt requires IV saline"Enter ?str_trim, ?str_pad in your R console to see further details.
Wrap lines into paragraphs
Use str_wrap() to wrap a long unstructured text into a structured paragraph with fixed line length. Provide the ideal character length for each line, and it applies an algorithm to insert newlines (\n) within the paragraph, as seen in the example below.
pt_course <- "Symptom onset 1/4/2020 vomiting chills fever. Pt saw traditional healer in home village on 2/4/2020. On 5/4/2020 pt symptoms worsened and was admitted to Lumta clinic. Sample was taken and pt was transported to regional hospital on 6/4/2020. Pt died at regional hospital on 7/4/2020."
str_wrap(pt_course, 40)
## [1] "Symptom onset 1/4/2020 vomiting chills\nfever. Pt saw traditional healer in\nhome village on 2/4/2020. On 5/4/2020\npt symptoms worsened and was admitted\nto Lumta clinic. Sample was taken and pt\nwas transported to regional hospital on\n6/4/2020. Pt died at regional hospital\non 7/4/2020."The base function cat() can be wrapped around the above command in order to print the output, displaying the new lines added.
cat(str_wrap(pt_course, 40))
## Symptom onset 1/4/2020 vomiting chills
## fever. Pt saw traditional healer in
## home village on 2/4/2020. On 5/4/2020
## pt symptoms worsened and was admitted
## to Lumta clinic. Sample was taken and pt
## was transported to regional hospital on
## 6/4/2020. Pt died at regional hospital
## on 7/4/2020.Often one must alter the case/capitalization of a string value, for example names of jursidictions. Use str_to_upper(), str_to_upper(), and str_to_title(), as shown below:
Using *base** R, the above can also be achieved with toupper(), tolower().
Title case Transforming the string so each word is capitalized can be achieved with str_to_title():
Use toTitleCase() from the tools package to achieve more nuanced capitalization (words like “to”, “the”, and “of” are not capitalized).
tools::toTitleCase("This is the US state of california")
## [1] "This is the US State of California"You can also use str_to_sentence(), which capitalizes only the first letter of the string.
Many stringr functions work to detect, locate, extract, match, replace, and split based on a specified pattern.
Use str_detect() as below. Note that by default the search is case sensitive!
The argument negate = can be included and set to TRUE if you want to know if the pattern is NOT present.
To ignore case/capitalization, wrap the pattern within regex() and within regex() add the argument ignore_case = T.
When str_detect() is applied to a character vector/column, it will return a TRUE/FALSE for each of the values in the vector.
# a vector/column of occupations
occupations <- c("field laborer",
"university professor",
"primary school teacher & tutor",
"tutor",
"nurse at regional hospital",
"lineworker at Amberdeen Fish Factory",
"physican",
"cardiologist",
"office worker",
"food service")
# Detect presence of pattern "teach" in each string - output is vector of TRUE/FALSE
str_detect(occupations, "teach")
## [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSEIf you need to count these, apply sum() to the output. This counts the number TRUE.
To search inclusive of multiple terms, include them separated by OR bars (|) within the pattern, as shown below:
If you need to make a long list of search terms, you can combine them using str_c() and sep = |, define this is a character object, and reference it later more succinctly. The example below includes possible occupation search terms for frontline medical providers.
# search terms
occupation_med_frontline <- str_c("medical", "medicine", "hcw", "healthcare", "home care", "home health",
"surgeon", "doctor", "doc", "physician", "surgery", "peds", "pediatrician",
"intensivist", "cardiologist", "coroner", "nurse", "nursing", "rn", "lpn",
"cna", "pa", "physician assistant", "mental health",
"emergency department technician", "resp therapist", "respiratory",
"phlebotomist", "pharmacy", "pharmacist", "hospital", "snf", "rehabilitation",
"rehab", "activity", "elderly", "subacute", "sub acute",
"clinic", "post acute", "therapist", "extended care",
"dental", "dential", "dentist", sep = "|")
occupation_med_frontline
## [1] "medical|medicine|hcw|healthcare|home care|home health|surgeon|doctor|doc|physician|surgery|peds|pediatrician|intensivist|cardiologist|coroner|nurse|nursing|rn|lpn|cna|pa|physician assistant|mental health|emergency department technician|resp therapist|respiratory|phlebotomist|pharmacy|pharmacist|hospital|snf|rehabilitation|rehab|activity|elderly|subacute|sub acute|clinic|post acute|therapist|extended care|dental|dential|dentist"This command returns the number of occupations which contain any one of the search terms for front-line medical providers (occupation_med_frontline):
Base R string search functions
The base function grepl() works similarly to str_detect(), in that it searches for matches to a pattern and returns a logical vector. The basic syntax is grepl(pattern, strings_to_search, ignore.case = FALSE, ...). One advantage is that the ignore.case argument is easier to write (there is no need to involve regex() function).
Likewise, the base functions sub() and gsub() act similarly to str_replace(). Their basic syntax is: gsub(pattern, replacement, strings_to_search, ignore.case = FALSE). sub() will replace the first instance of the pattern, whereas gsub() will replace all instances of the pattern.
Within case_when()
str_detect() is often used within case_when() (from dplyr). Let’s say the occupations are a column in the linelist called occupations. The mutate() below creates a new column called is_educator by using conditional logic via case_when(). See the page on data cleaning to learn more about case_when().
df <- df %>%
mutate(is_educator = case_when(
# term search within occupation, not case sensitive
str_detect(occupations,
regex("teach|prof|tutor|university",
ignore_case = TRUE)) ~ "Educator",
# all others
TRUE ~ "Not an educator"))As a reminder, it may be important to add exclusion criteria to the conditional logic (negate = F):
df <- df %>%
# value in new column is_educator is based on conditional logic
mutate(is_educator = case_when(
# occupation column must meet 2 criteria to be assigned "Educator":
# it must have a search term AND NOT any exclusion term
# Must have a search term AND
str_detect(occupations,
regex("teach|prof|tutor|university", ignore_case = T)) &
# Must NOT have an exclusion term
str_detect(occupations,
regex("admin", ignore_case = T),
negate = T) ~ "Educator"
# All rows not meeting above criteria
TRUE ~ "Not an educator"))To locate the first position of a pattern, use str_locate(). It outputs a start and end position.
Like other str functions, there is an "_all" version (str_locate_all()) which will return the positions of all instances of the pattern within each string. This outputs as a list.
phrases <- c("I wish", "I hope", "he hopes", "He hopes")
str_locate(phrases, "h" ) # position of *first* instance of the pattern
## start end
## [1,] 6 6
## [2,] 3 3
## [3,] 1 1
## [4,] 4 4
str_locate_all(phrases, "h" ) # position of *every* instance of the pattern
## [[1]]
## start end
## [1,] 6 6
##
## [[2]]
## start end
## [1,] 3 3
##
## [[3]]
## start end
## [1,] 1 1
## [2,] 4 4
##
## [[4]]
## start end
## [1,] 4 4str_extract_all() returns the matching patterns themselves, which is most useful when you have offered several patterns via “OR” conditions. For example, looking in the string vector of occupations (see previous tab) for either “teach”, “prof”, or “tutor”.
str_extract_all() returns a list which contains all matches for each evaluated string. See below how occupation 3 has two pattern matches within it.
str_extract_all(occupations, "teach|prof|tutor")
## [[1]]
## character(0)
##
## [[2]]
## [1] "prof"
##
## [[3]]
## [1] "teach" "tutor"
##
## [[4]]
## [1] "tutor"
##
## [[5]]
## character(0)
##
## [[6]]
## character(0)
##
## [[7]]
## character(0)
##
## [[8]]
## character(0)
##
## [[9]]
## character(0)
##
## [[10]]
## character(0)str_extract() extracts only the first match in each evaluated string, producing a character vector with one element for each evaluated string. It returns NA where there was no match. The NAs can be removed by wrapping the returned vector with na.exclude(). Note how the second of occupation 3’s matches is not shown.
Subset, Count
Aligned functions include str_subset() and str_count().
str_subset() returns the actual values which contained the pattern:
str_subset(occupations, "teach|prof|tutor")
## [1] "university professor" "primary school teacher & tutor"
## [3] "tutor"`str_count() returns a vector of numbers: the number of times a search term appears in each evaluated value.
To split a string based on a pattern, use str_split(). It evaluates the strings and returns a list of character vectors consisting of the newly-split values.
The simple example below evaluates one string, and produces a list with one element - a character vector with three values:
str_split("jaundice, fever, chills", ",", simplify = T)
## [,1] [,2] [,3]
## [1,] "jaundice" " fever" " chills"You can assign this as a named object, and access the nth symptom:
pt1_symptoms <- str_split("jaundice, fever, chills", ",", simplify = T)
pt1_symptoms[2]
## [1] " fever"If multiple strings are evaluated, there will be more than one element in the returned list.
symptoms <- c("jaundice, fever, chills", # patient 1
"chills, aches, pains", # patient 2
"fever", # patient 3
"vomiting, diarrhoea", # patient 4
"bleeding from gums, fever", # patient 5
"rapid pulse, headache") # patient 6
str_split(symptoms, ",") # split each patient's symptoms
## [[1]]
## [1] "jaundice" " fever" " chills"
##
## [[2]]
## [1] "chills" " aches" " pains"
##
## [[3]]
## [1] "fever"
##
## [[4]]
## [1] "vomiting" " diarrhoea"
##
## [[5]]
## [1] "bleeding from gums" " fever"
##
## [[6]]
## [1] "rapid pulse" " headache"To access a specific symptom you can use syntax like this: the_split_return_object[[2]][1], which would access the first symptom from the second evaluated string (“chills”). See the R basics page for more detail on accessing elements.
To return a “character matrix” instead, which may be useful if creating dataframe columns, set the argument simplify = TRUE as shown below:
str_split(symptoms, ",", simplify = T)
## [,1] [,2] [,3]
## [1,] "jaundice" " fever" " chills"
## [2,] "chills" " aches" " pains"
## [3,] "fever" "" ""
## [4,] "vomiting" " diarrhoea" ""
## [5,] "bleeding from gums" " fever" ""
## [6,] "rapid pulse" " headache" ""You can also adjust the number of splits to create with the n = argument. For example, this restricts the number of splits (from the left side) to 2 splits. The further commas remain within the second split.
str_split(symptoms, ",", simplify = T, n = 2)
## [,1] [,2]
## [1,] "jaundice" " fever, chills"
## [2,] "chills" " aches, pains"
## [3,] "fever" ""
## [4,] "vomiting" " diarrhoea"
## [5,] "bleeding from gums" " fever"
## [6,] "rapid pulse" " headache"Note - the same outputs can be achieved with str_split_fixed(), in which you do not* give the simplify argument, but must instead designate the number of columns (n).*
Splitting a column within a dataframe
Within a dataframe, to split one character column into other columns use use separate() from dplyr.
If we have a simple dataframe df consisting of a case ID column, one character column with symptoms, and one outcome column:
First provide the column to be separated, then provide a vector (c()) of new columns names to the argument into =, as shown below. The argument sep = can be a character, or a number (interpreted as the character position to split at).
Optional arguments include remove = (FALSE by default, removes the input column) and convert = (FALSE by default, will cause string “NA”s to become NA).
extra = will control what happens if there are more many values created by the separation than new columns named. Setting extra equal to "warn" means R will return a warning but proceed and drop the values (the default). "drop" means the values will be dropped with no warning.
Setting extra = "merge" will only split to the number of new columns listed in into - this setting will preserve all your data.
# third symptoms combined into second new column
df %>%
separate(symptoms, into = c("sym_1", "sym_2"), sep=",", extra = "merge")
## case_ID sym_1 sym_2 outcome
## 1 1 jaundice fever, chills Success
## 2 2 chills aches, pains Failure
## 3 3 fever <NA> Failure
## 4 4 vomiting diarrhoea Success
## 5 5 bleeding from gums fever Success
## 6 6 rapid pulse headache Success# third symptoms are lost
df %>%
separate(symptoms, into = c("sym_1", "sym_2", "sym_3"), sep=",")
## case_ID sym_1 sym_2 sym_3 outcome
## 1 1 jaundice fever chills Success
## 2 2 chills aches pains Failure
## 3 3 fever <NA> <NA> Failure
## 4 4 vomiting diarrhoea <NA> Success
## 5 5 bleeding from gums fever <NA> Success
## 6 6 rapid pulse headache <NA> Success# third symptoms given their own column
separated <- df %>%
separate(symptoms, into = c("sym_1", "sym_2", "sym_3"), sep=",")
separated
## case_ID sym_1 sym_2 sym_3 outcome
## 1 1 jaundice fever chills Success
## 2 2 chills aches pains Failure
## 3 3 fever <NA> <NA> Failure
## 4 4 vomiting diarrhoea <NA> Success
## 5 5 bleeding from gums fever <NA> Success
## 6 6 rapid pulse headache <NA> SuccessCAUTION: If you do not provide enough into values for the new columns, your data may be truncated.
One solution to automatically make as many columns as needed could be:
unite()
Within a dataframe, bringing together multiple columns (the opposite of separate()) can be achieved with unite() from tidyr.
Provide the name of the new united column. Then provide the names of the columns you wish to unite. By default the separator used in the united column is "_", but this can be changed with the sep argument. Other optional arguments include remove = (TRUE by default, removes the input columns from the data frame), and na.rm = (FALSE by default, it removes missing values while uniting).
Below, we re-unite the dataframe that was separated above.
separated %>%
unite(
col = "all_symptoms", # name of the new united column
c("sym_1", "sym_2", "sym_3"), # columns to unite
sep = ", ", # separator to use in united column
remove = TRUE, # if TRUE, removes input cols from the data frame
na.rm = TRUE # if TRUE, missing values are removed before uniting
)
## case_ID all_symptoms outcome
## 1 1 jaundice, fever, chills Success
## 2 2 chills, aches, pains Failure
## 3 3 fever Failure
## 4 4 vomiting, diarrhoea Success
## 5 5 bleeding from gums, fever Success
## 6 6 rapid pulse, headache SuccessGroups within strings
str_match() TBD
Regular expressions, or “regex”, is a concise language for describing patterns in strings.
Much of this tab is adapted from this tutorial and this cheatsheet
Backslash \ as escape
The backslash \ is used to “escape” the meaning of the next character. This way, a backslash can be used to have a quote mark display within other quote marks (\") - the middle quote mark will not “break” the surrounding quote marks.
Note - thus, if you want to display a backslash, you must escape it’s meaning with *another backslash. So you must write two backslashes \\ to display one.
Special characters
| Special character | Represents |
|---|---|
"\\" |
backslash |
"\n" |
a new line (newline) |
"\"" |
double-quote within double quotes |
'\'' |
single-quote within single quotes |
"\“| grave accent”| carriage return“| tab”| vertical tab"` |
backspace |
Run ?"'" in the R Console to display a complete list of these special characters (it will appear in the RStudio Help pane).
If you are not familiar with it, a regular expression can look like an alien language:
A regular expression is applied to extract specific patterns from unstructured text - for example medical notes, chief complaint, matient history, or other free text columns in a dataset.
There are four basic tools one can use to create a basic regular expression:
Character sets
Character sets, are a way of expressing listing options for a character match, within brackets. So any a match will be triggered if any of the characters within the brackets are found in the string. For example, to look for vowels one could use this character set: “[aeiou]”. Some other common character sets are:
| Character set | Matches for |
|---|---|
"[A-Z]" |
any single capital letter |
"[a-z]" |
any single lowercase letter |
"[0-9]" |
any digit |
[:alnum:] |
any alphanumeric character |
[:digit:] |
any numeric digit |
[:alpha:] |
any letter (upper or lowercase) |
[:upper:] |
any uppercase letter |
[:lower:] |
any lowercase letter |
Character sets can be combined within one bracket (no spaces!), such as "[A-Za-z]" (any upper or lowercase letter), or another example "[t-z0-5]" (lowercase t through z OR number 0 through 5).
Meta characters
Meta characters are shorthand for character sets. Some of the important ones are listed below:
| Meta character | Represents |
|---|---|
"\\s" |
a single space |
"\\w" |
any single alphanumeric character (A-Z, a-z, or 0-9) |
"\\d" |
any single numeric digit (0-9) |
Quantifiers
Typically you do not want to search for a match on only one character. Quantifiers allow you to designate the length of letters/numbers to allow for the match.
Quantifiers are numbers written within curly brackets { } after the character they are quantifying, for example,
"A{2}" will return instances of two capital A letters."A{2,4}" will return instances of between two and four capital A letters (do not put spaces!)."A{2,}" will return instances of two or more capital A letters."A+" will return instances of one or more capital A letters (group extended until a different character is encountered).* asterisk to return zero or more matches (useful if you are not sure the pattern is present)Using the + plus symbol as a quantifier, the match will occur until a different character is encountered. For example, this expression will return all words (alpha characters: "[A-Za-z]+"
When a quantifier of {2} is used, only pairs of consecutive A’s are returned. Two pairs are identified within AAAA.
When a quantifier of {2,4} is used, groups of consecutive A’s that are two to four in length are returned.
With the quantifier +, groups of one or more are returned:
Relative position
These express requirements for what precedes or follows a pattern. For example, to extract sentences, “two numbers that are followed by a period” (""). (?<=\.)\s(?=[A-Z])
| Position statement | Matches to |
|---|---|
"(?<=b)a" |
“a” that is preceded by a “b” |
"(?<!b)a" |
“a” that is NOT preceded by a “b” |
"a(?=b)" |
“a” that is followed by a “b” |
"a(?!b)" |
“a” that is NOT followed by a “b” |
Groups
Capturing groups in your regular expression is a way to have a more organized output upon extraction.
Regex examples
Below is a free text for the examples. We will try to extract useful information from it using a regular expression search term.
pt_note <- "Patient arrived at Broward Hospital emergency ward at 18:00 on 6/12/2005. Patient presented with radiating abdominal pain from LR quadrant. Patient skin was pale, cool, and clammy. Patient temperature was 99.8 degrees farinheit. Patient pulse rate was 100 bpm and thready. Respiratory rate was 29 per minute."This expression matches to all words (any character until hitting non-character such as a space):
str_extract_all(pt_note, "[A-Za-z]+")
## [[1]]
## [1] "Patient" "arrived" "at" "Broward" "Hospital"
## [6] "emergency" "ward" "at" "on" "Patient"
## [11] "presented" "with" "radiating" "abdominal" "pain"
## [16] "from" "LR" "quadrant" "Patient" "skin"
## [21] "was" "pale" "cool" "and" "clammy"
## [26] "Patient" "temperature" "was" "degrees" "farinheit"
## [31] "Patient" "pulse" "rate" "was" "bpm"
## [36] "and" "thready" "Respiratory" "rate" "was"
## [41] "per" "minute"The expression "[0-9]{1,2}" matches to consecutive numbers that are 1 or 2 digits in length. It could also be written "\\d{1,2}", or "[:digit:]{1,2}".
str_extract_all(pt_note, "[0-9]{1,2}")
## [[1]]
## [1] "18" "00" "6" "12" "20" "05" "99" "8" "10" "0" "29"str_split(pt_note, ".")
## [[1]]
## [1] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [26] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [51] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [76] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [101] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [126] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [151] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [176] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [201] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [226] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [251] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [276] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
## [301] "" "" "" "" "" "" "" "" ""This expression will extract all sentences (assuming first letter is capitalized, and the sentence ends with a period). The pattern reads in English as: "A capital letter followed by some lowercase letters, a space, some letters, a space,
You can view a useful list of regex expressions and tips on page 2 of this cheatsheet
Also see this tutorial.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
knitr::kable DT
For publication
quickly changing the denominator (per 100,000, etc.)
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Note the argument na.rm=T, which removes missing values from the calculation.
If missing values are not excluded, the returned value will be NA (missing).
Note the argument na.rm=T, which removes missing values from the calculation.
If missing values are not excluded, the returned value will be NA (missing).
Note the argument na.rm=T, which removes missing values from the calculation.
If missing values are not excluded, the returned value will be NA (missing).
Note the argument na.rm=T, which removes missing values from the calculation.
If missing values are not excluded, the returned value will be NA (missing).
Note the argument na.rm=T, which removes missing values from the calculation.
If missing values are not excluded, the returned value will be NA (missing).
Frequency table of 1 and 2 categorical variables
A table with 3 variables
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
quant/quant, quant/cat, cat/cat t-tests odds ratios, mantel-haensel, etc.
Keep the title of this section as “Preparation”.
Data preparation steps such as:
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
This code chunk shows the loading of packages required for the analyses.
pacman::p_load(rio, # File import
here, # File locator
tidyverse, # data management + ggplot2 graphics
aweek, # working with dates
lubridate, # Manipulate dates
incidence, # an option for epicurves of linelist data
stringr, # Search and manipulate character strings
forcats, # working with factors
RColorBrewer) # Color palettes from colorbrewer2.orgTwo example datasets are used in this section:
The dataset is imported using the import() function from the rio package. See the page on importing data for various ways to import data. The linelist and aggregated versions of the data are displayed below.
For most of this document, the linelist dataset will be used. The aggregated counts dataset will be used at the end.
Review the two datasets and notice the differences
Case linelist
The first 50 rows are displayed
Case counts aggregated by hospital
The first 50 rows are displayed
You may want to set certain parameters for production of a report, such as the date for which the data is current (the “data date”). You can then reference the data_date in the code when applying filters or in captions that auto-update.
Verify that each relevant date column is class Date and has an appropriate range of values. This for loop prints a histogram for each column.
# create character vector of column names
DateCols <- as.character(tidyselect::vars_select(names(linelist), matches("date|Date|dt")))
# Produce histogram of each date column
for (Col in DateCols) { # open loop. iterate for each name in vector DateCols
hist(linelist[, Col], # print histogram of the column in linelist dataframe
breaks = 50, # number of breaks for the histogram
xlab = Col) # x-axis label is the name of the column
} # close the loopincidence packageincidence package
Below are tabs on making quick epicurves using the incidence package
CAUTION: Epicontacts expects data to be in a “linelist” format of one row per case (not aggregated). If your data is aggregated counts, look to the ggplot epicurves tab.
TIP: The documentation for plotting an incidence object can be accessed by entering ?plot.incidence in your R console.
2 steps are requires to plot an epicurve with the incidence package:
incidence())
A simple example - an epicurve of daily cases:
# load incidence package
library(incidence)
# create the incidence object using data by day
epi_day <- incidence(linelist$date_onset, # the linelist data
interval = "day") # the time interval
# plot the incidence object
plot(epi_day)Change time interval of case aggregation (bars)
The interval argument defines how the observations are grouped. Available options include all the options from the package aweek, including but not limited to:
Below are examples of how different intervals look when applied to the linelist.
Format and frequency of the date labels on the x-axis are the defaults for the specified interval.
# Create the incidence objects (with different intervals)
##############################
# Weekly (Monday week by default)
epi_wk <- incidence(linelist$date_onset, interval = "Monday week")
# Sunday week
epi_Sun_wk <- incidence(linelist$date_onset, interval = "Sunday week")
# Three weeks (Monday weeks by default)
epi_3wk <- incidence(linelist$date_onset, interval = "3 weeks")
# Monthly
epi_month <- incidence(linelist$date_onset, interval = "month")
# Plot the incidence objects (+ titles for clarity)
############################
plot(epi_wk)+ labs(title = "Monday weeks")
plot(epi_Sun_wk)+ labs(title = "Sunday weeks")
plot(epi_3wk)+ labs(title = "Every 3 Monday weeks")
plot(epi_month)+ labs(title = "Months")The incidence package enables modifications in the following ways:
plot() (e.g. show_cases, col_pal, alpha…)scale_x_incidence() and make_labels()ggplot() additions via the + operatorRead details in the Help files by entering ?scale_x_incidence and ?plot.incidence in the R console. Online vignettes are listed in the resources tab.
plot() modificationsplot() modifications
A incidence plot can be modified in the following ways. Type ?plot.incidence in the R console for more details.
show_cases = If TRUE, each case is shows as a box. Best on smaller outbreaks.color = Color of case bars/boxesborder = Color of line around boxes, if show_cases = TRUEalpha = Transparency of case bars/boxes (1 is fully opaque, 0 is fully transparent)xlab = Title of x-axis (axis labels can also be applied using labs() from ggplot)ylab = Title of y-axis; defaults to user-defined incidence time intervallabels_week = Logical, indicate whether x-axis labels are in week or date format, absent other modificationsn_breaks = Number of x-axis label breaks, absent other modificationsfirst_date, last_date Dates used to trim the plotSee examples of these arguments in the subsequent tabs.
To plot the epicurve of a subset of data:
incidence() commandThe example below uses data filtered to show only cases at Central Hospital.
# filter the dataset
central_data <- linelist %>%
filter(hospital == "Central Hospital")
# create incidence object using subset of data
central_outbreak <- incidence(central_data$date_onset, interval = "week")
# plot
plot(central_outbreak) + labs(title = "Weekly case incidence at Central Hospital")TIP: Remember that date-axis labels are independent from the aggregation of the data into bars
Modify the bars
The aggregation of data into bars occurs when you set the interval = when creating the incidence object. The options for interval come from the package aweek and include options like “day”, “Monday week”, “Sunday week”, “month”, “2 weeks”, etc. See the incidence intro tab for more information.
Modify date-axis labels (frequency & format)
If working with the incidence package, you have several options to make these modifications. Some utilize the incidence package functions scale_x_date() and make_breaks(), others use the ggplot2 function scale_x_date(), and others use a combination.
DANGER: Be cautious setting the y-axis scale breaks (e.g. 0 to 30 by 5: seq(0, 30, 5)). Static numbers can cut-off your data if the data changes!.
scale_x_incidence() onlyscale_x_incidence() from the incidence package:
interval (e.g. Sundays or Mondays)n_breaks specify number of date labels, which start from the interval of the first case.
n_breaks = nrow(i)/n (“i” is the incidence object name and “n” is a number)labels_week labels formatted as either weeks (YYYY-Www) or dates (YYYY-MM-DD)Other notes:
?scale_x_incidence into the R console to see more information.scale_x_date() to the plot will remove labels created by scale_x_incidence# create weekly incidence object (Sunday weeks)
i <- incidence(central_data$date_onset, interval = "Sunday week")
plot(i)+
scale_x_incidence(i, # name of incidence object
labels_week = F, # show dates instead of weeks
n_breaks = nrow(i)/8) # breaks every 8 weeks from week of first casescale_x_date() and make_breaks()scale_x_date() from ggplot2, but also leverage make_breaks() from incidence:
make_breaks() to define date label breaks
make_breaks() is similar to scale_x_incidence() (described above). Provide the incidence object name and optionally n_breaks as described before.scale_x_date() to the plot:
breaks = provide the breaks vector you created with make_breaks(), followed by $breaks (see example below)date_labels = provide a format for the date labels (e.g. “%d %b”) (use “” for new line)# Break modification using scale_x_date() and make_breaks()
###########################################################
# make incidence object
i <- incidence(central_data$date_onset, interval = "Monday week")
# make breaks
i_labels <- make_breaks(i, n_breaks = nrow(i)/6) # using interval from i, breaks every 6 weeks
# plot
plot(i)+
scale_x_date(breaks = i_labels$breaks, # call the breaks
date_labels = "%d\n%b '%y", # date format
date_minor_breaks = "weeks") # gridlines each week (aligns with Sundays only) scale_x_date() onlyscale_x_date() only
date_breaks = (e.g. “day”, “week”, “2 weeks”, “month”, “year”)date_minor_breaks = for vertical lines between date labelsbreaks = and to minor_breaks =date_labels = for formatting (see Dates page for tips)expand = c(0,0) to start labels at the first incidence bar. Otherwise, first label will shift depending on your specified label interval.*Note: if using aggregated counts (for example an epiweek x-axis) your x-axis may not be Date class and may require use scale_x_discrete() instead of scale_x_date() - see ggplot tips page for more details.
# Break modification using scale_x_date() only
##############################################
# make incidence object
i <- incidence(central_data$date_onset, interval = "Monday week")
# plot
plot(i)+
scale_x_date(expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "3 weeks", # labels appear every 3 Monday weeks
date_minor_breaks = "week", # vertical lines appear every Monday week
date_labels = "%d\n%b\n'%y") # date labels format If you want a plot of Sunday weeks and also finely-adjusted label formats, you might find a code example helpful.
Here is an example of producing a weekly epicurve using incidence for Sunday weeks, with finely-adjusted date labels through scale_x_date():
# load packages
pacman::p_load(tidyverse, # for ggplot
incidence, # for epicurve
lubridate) # for floor_date() and ceiling_date()
# create incidence object (specifying SUNDAY weeks)
central_outbreak <- incidence(central_data$date_onset, interval = "Sunday week") # equivalent to "MMWRweek" (see US CDC)
# plot() the incidence object
plot(central_outbreak)+
### ggplot() commands added to the plot
# scale modifications
scale_x_date(
expand = c(0,0), # remove excess x-axis space below and after case bars
# sequence by 3 weeks, from Sunday before first case to Sunday after last case
breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 7)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 7)),
by = "3 weeks"),
# sequence by week, from Sunday before first case to Sunday after last case
minor_breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 7)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 7)),
by = "7 days"),
# date labels
date_labels = "%d\n%b'%y")+ # adjust how dates are displayed
scale_y_continuous(
expand = c(0,0), # remove excess space under x-axis
breaks = seq(0, 30, 5))+ # adjust y-axis intervals
# Aesthetic themes
theme_minimal()+ # simplify background
theme(
axis.title = element_text(size = 12, face = "bold"), # axis titles formatting
plot.caption = element_text(face = "italic", hjust = 0))+ # caption formatting, left-aligned
# Plot labels
labs(x = "Week of symptom onset (Sunday weeks)",
y = "Weekly case incidence",
title = "Weekly case incidence at Central Hospital",
#subtitle = "",
caption = stringr::str_glue("n = {nrow(central_data)} from Central Hospital; Case onsets range from {format(min(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')} to {format(max(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')}\n{nrow(central_data %>% filter(is.na(date_onset)))} cases missing date of onset and not shown"))To show boxes around each individual case, use the argument show_cases = TRUE in the plot() function.
Boxes around each case can be more reader-friendly, if the outbreak is of a small size. Boxes can be applied when the interval is days, weeks, or any other time period. The code below creates the weekly epicurve for a smaller outbreak (only cases from Central Hospital), with boxes around each case.
# create filtered dataset for Central Hospital
central_data <- linelist %>%
filter(hospital == "Central Hospital")
# create incidence object (weekly)
central_outbreak <- incidence(central_data$date_onset, interval = "Monday week")
# plot outbreak
plot(central_outbreak,
show_cases = T) # show boxes around individual casesThe same epicurve showing individual cases, but with other aesthetic modifications:
# add plot() arguments and ggplot() commands
plot(central_outbreak,
show_cases = T, # show boxes around each individual case
color = "lightblue", # color inside boxes
border = "darkblue", # color of border around boxes
alpha = 0.5)+ # transparency
### ggplot() commands added to the plot
# scale modifications
scale_x_date(
expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "4 weeks", # labels appear every 4 Monday weeks
date_minor_breaks = "week", # vertical lines appear every Monday week
date_labels = "%d\n%b'%y")+ # date labels format
scale_y_continuous(
expand = c(0,0), # remove excess space under x-axis
breaks = seq(0, 35, 5))+ # adjust y-axis intervals
# aesthetic themes
theme_minimal()+ # simplify background
theme(
axis.title = element_text(size = 12, face = "bold"), # axis title format
plot.caption = element_text(face = "italic", hjust = 0))+ # caption format and left-align
# plot labels
labs(x = "Week of symptom onset (Monday weeks)",
y = "Weekly reported cases",
title = "Weekly case incidence at Central Hospital",
#subtitle = "",
caption = stringr::str_glue("n = {nrow(central_data)} from Central Hospital; Case onsets range from {format(min(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')} to {format(max(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')}\n{nrow(central_data %>% filter(is.na(date_onset)))} cases missing date of onset and not shown"))To color the cases by value, provide the column to the groups = argument in the incidence() command. In the example below the cases are colored by their age category. Note the use of incidence() argument na_as_group =. If TRUE (by default) missing values (NA) will form their own group.
# Create incidence object, with data grouped by age category
age_outbreak <- incidence(linelist$date_onset, # date of onset for x-axis
interval = "week", # weekly aggregation of cases
groups = linelist$age_cat, # color by age_cat value
na_as_group = TRUE) # missing values assigned their own group
# plot the epicurve
plot(age_outbreak) Adjusting order
To adjust the order of group appearance (on plot and in legend), the group column must be class Factor. Adjust the order by adjusting the order of the levels (including NA). Below is an example with gender groups using data from Central Hospital only.
NA first, so it appears on the top of the barsexclude = NULL in factor() is necessary to adjust the order of NA, which is excluded by default.fill = in labs()You can read more about factors in their page (LINK)
# Create incidence object, data grouped by gender
#################################################
# Classify "gender" column as factor
####################################
# with specific level order and labels, includin for missing values
central_data <- linelist %>%
filter(hospital == "Central Hospital") %>%
mutate(gender = factor(gender,
levels = c(NA, "f", "m"),
labels = c("Missing", "Female", "Male"),
exclude = NULL))
# Create incidence object, by gender
####################################
gender_outbreak_central <- incidence(central_data$date_onset,
interval = "week",
groups = central_data$gender,
na_as_group = TRUE) # Missing values assigned their own group
# plot epicurve with modifications
##################################
plot(gender_outbreak_central,
show_cases = TRUE)+ # show box around each case
### ggplot commands added to plot
# scale modifications
scale_x_date(expand = c(0,0),
date_breaks = "6 weeks",
date_minor_breaks = "week",
date_labels = "%d %b\n%Y")+
# aesthetic themes
theme_minimal()+ # simplify plot background
theme(
legend.title = element_text(size = 14, face = "bold"),
axis.title = element_text(face = "bold"))+ # axis title bold
# plot labels
labs(fill = "Gender", # title of legend
title = "Show case boxes, with modifications",
y = "Weekly case incidence",
x = "Week of symptom onset") To change the legend
Use ggplot() commands such as:
theme(legend.position = "top") (or “bottom”, “left”, “right”)theme(legend.direction = "horizontal")theme(legend.title = element_blank()) to have no titleSee the page of ggplot() tips for more details on legends.
To specify colors manually, provide the name of the color or a character vector of multiple colors to the argument color =. Note to function properly the number of colors listed must equal the number of groups (be aware of missing values as a group)
# weekly outbreak by hospital
hosp_outbreak <- incidence(linelist$date_onset,
interval = "week",
groups = linelist$hospital,
na_as_group = FALSE) # Missing values not assigned their own group
# default colors
plot(hosp_outbreak)
# manual colors
plot(hosp_outbreak, color = c("darkgreen", "darkblue", "purple", "grey", "yellow", "orange"))To change the color palette
Use the argument col_pal in plot() to change the color palette to one of the default base R palettes (do not put the name of the palette in quotes).
Other palettes include TO DO add page with palette names… To DO
# Create incidence object, with data grouped by age category
age_outbreak <- incidence(linelist$date_onset, # date of onset for x-axis
interval = "week", # weekly aggregation of cases
groups = linelist$age_cat, # color by age_cat value
na_as_group = TRUE) # missing values assigned their own group
# plot the epicurve
plot(age_outbreak)
# plot with different color palette
plot(age_outbreak, col_pal = rainbow)To facet the plot by a variable (make “small multiples”), see the tab on epicurves with ggplot()
ggplot()ggplot()
Below are tabs on using the ggplot2 package to produce epicurves from a linelist dataset.
Unlike using incidence package, you must manually control the aggregation of the data (into weeks, months, etc) and the labels on the date axis. If not carefully managed, this can lead to many headaches.
These tabs use a subset of the linelist dataset - only the cases from Central Hospital.
To produce an epicurve with ggplot() there are three main elements:
Below is perhaps the most simple code to produce daily and weekly epicurves. Axis scales and labels use default options.
# daily
ggplot(data = central_data, aes(x = date_onset)) + # x column must be class Date
geom_histogram(binwidth = 1)+ # date values binned by 1 day
labs(title = "Daily")
# weekly
ggplot(data = central_data, aes(x = date_onset)) +
geom_histogram(binwidth = 7)+ # date values binned each 7 days (arbitrary 7 days!)
labs(title = "Weekly") CAUTION: Using
binwidth = 7 starts the first bin at the first case, which could be any day of the week! To create specific Monday or Sunday weeks, see below .
To create weekly epicurves where the bins begin on a specific day of the week (e.g. Monday, Sunday), specify the histogram breaks = manually (not binwidth). This can be done by creating a sequence of dates using seq.Date() from base R. You can start/end the sequence at a specific date (as.Date("YYYY-MM-DD"), or write flexible code to begin the sequence at a specific day of the week before the first case. An example of creating such weekly breaks is below:
seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 1)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 1)),
by = "7 days")To achieve the “from” value (earliest date of the sequence), the minimum value in the column date_onset is fed to floor_date() from the lubridate package, which according to the above specified arguments produces the start date of that “week”, given that the start of each week is a Monday (week_start = 1). Likewise, the “to” value (end date of the sequence) is specified using the inverse ceiling_date() function to produce the Monday after the last case. The “by” argument can be set to any length of days, weeks, or months.
This code is applied to create the histogram breaks, and also the breaks for the date labels. Read more about the date labels in the Modifications tab. Defining your breaks like above will be necessary if your weekly bins are not by Monday weeks.
Below is detailed code to produce weekly epicurves for Monday and Sunday weeks. See the tab on Modifications (axes) to learn the nuances of date-axis label management.
Monday weeks
Of note:
week_start = 1) before the earliest case and to end the Monday after the last case (see explanation above).date_breaks = within scale_x_date(), which also uses Monday weeks. Sunday weeks use a different method.date_minor_breaks = within scale_x_date(), again because this plot is for Monday weeks. Sunday weeks use a different method.expand = c(0,0) to the x and y scales removes excess space on each side of the plot, which also ensures the labels begin at the first bar.geom_histogram()# TOTAL MONDAY WEEK ALIGNMENT
#############################
ggplot(central_data, aes(x = date_onset)) +
# make histogram: specify bin break points: starts the Monday before first case, end Monday after last case
geom_histogram(
breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 1)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 1)),
by = "7 days"), # bins are 7-days
color = "darkblue", # color of lines around bars
fill = "lightblue") + # color of fill within bars
# x-axis labels
scale_x_date(expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "3 weeks", # labels appear every 3 Monday weeks
date_minor_breaks = "week", # vertical lines appear every Monday week
date_labels = "%d\n%b\n'%y")+ # date labels format
# y-axis
scale_y_continuous(expand = c(0,0))+ # remove excess y-axis space between bottom of bars and the labels
# aesthetic themes
theme_minimal()+ # a set of themes to simplify plot
theme(plot.caption = element_text(face = "italic", hjust = 0), # caption on left side in italics
axis.title = element_text(face = "bold"))+ # axis titles in bold
# labels
labs(title = "Weekly incidence of cases (Monday weeks)",
subtitle = "Subtitle: Note alignment of bars, vertical lines, and axis labels on Mondays",
x = "Week of symptom onset",
y = "Weekly incident cases reported",
caption = stringr::str_glue("n = {nrow(central_data)} from Central Hospital; Case onsets range from {format(min(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')} to {format(max(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')}\n{nrow(central_data %>% filter(is.na(date_onset)))} cases missing date of onset and not shown"))Sunday weeks
The below code creates a histogram of the rows, using a date column as the x-axis. Of note:
week_start = 7) before the earliest case and to end the Monday after the last case (see explanation above).breaks = and minor_breaks = within scale_x_date(). You cannot use the scale_x_date() arguments of date_breaks and date_minor_breaks as these align with Monday weeks.expand = c(0,0) to the x and y scales removes excess space on each side of the plot, which also ensures the labels begin at the first bar.geom_histogram()# TOTAL SUNDAY WEEK ALIGNMENT
#############################
ggplot(central_data, aes(x = date_onset)) +
# For histogram, manually specify bin break points: starts the Sunday before first case, end Sunday after last case
geom_histogram(
breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 7)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 7)),
by = "7 days"), # bins are 7-days
color = "darkblue", # color of lines around bars
fill = "lightblue") + # color of fill within bars
# The labels on the x-axis
scale_x_date(expand = c(0,0),
breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 7)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 7)),
by = "3 weeks"),
minor_breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 7)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 7)),
by = "7 days"),
date_labels = "%d\n%b\n'%y")+ # day, above month abbrev., above 2-digit year
# y-axis
scale_y_continuous(expand = c(0,0))+ # removes excess y-axis space between bottom of bars and the labels
# aesthetic themes
theme_minimal()+ # a set of themes to simplify plot
theme(plot.caption = element_text(face = "italic", hjust = 0), # caption on left side in italics
axis.title = element_text(face = "bold"))+ # axis titles in bold
# labels
labs(title = "Weekly incidence of cases (Sunday weeks)",
subtitle = "Subtitle: Note alignment of bars, vertical lines, and axis labels on Sundays",
x = "Week of symptom onset",
y = "Weekly incident cases reported",
caption = stringr::str_glue("n = {nrow(central_data)} from Central Hospital; Case onsets range from {format(min(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')} to {format(max(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')}\n{nrow(central_data %>% filter(is.na(date_onset)))} cases missing date of onset and not shown"))TIP: Remember that date-axis labels are independent from the aggregation of the data into bars
To modify the aggregation of data into bins/bars, do one of the following:
binwidth = within geom_histogram() - for a column of class Date, the given number is interpreted in daysbreaks = as a sequence of bin break-point datesggplot(). See the tab on aggregated counts for more information.To modify the date labels, use scale_x_date() in one of these ways:
date_breaks = to specify label frequency (e.g. “day”, “week”, “3 weeks”, “month”, or “year”)date_minor_breaks = to specify frequency of minor vertical gridlines between date labelsexpand = c(0,0) to begin the labels at the first bar (otherwise, first label will shift forward depending on specified frequency)date_labels = to specify format of date labels - see the Dates page for tips (use \n for a new line)breaks = and minor_breaks = by providing a sequence of dates for breaksdate_labels = for formatting as described aboveTo create a sequence of dates
You can use seq.Date() from base R. You can start/end the sequence at a specific date (as.Date("YYYY-MM-DD"), or write flexible code to begin the sequence at a specific day of the week before the first case. An example of creating such flexible breaks is below:
seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 1)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 1)),
by = "7 days")To achieve the “from” value (earliest date of the sequence), the minimum value in the column date_onset is fed to floor_date() from the lubridate package, which according to the above specified arguments produces the start date of that “week”, given that the start of each week is a Monday (week_start = 1). Likewise, the “to” value (end date of the sequence) is specified using the inverse ceiling_date() function to produce the Monday after the last case. The “by” argument can be set to any length of days, weeks, or months.
If using aggregated counts (for example an epiweek x-axis) your x-axis may not be Date class and may require use scale_x_discrete() instead of scale_x_date() - see ggplot tips page for more details.
Set maximum and minimum date values using limits = c() within scale_x_date(). E.g. scale_x_date(limits = c(as.Date("2014-04-01), NA)) sets a minimum but leaves the maximum open.
CAUTION: Caution using limits! They remove all data outside the limits, which can impact y-axis max/min, modeling, and other statistics. Strongly consider instead using limits by adding coord_cartesian() to your plot, which acts as a “zoom” without removing data.
DANGER: Be cautious setting the y-axis scale breaks (e.g. 0 to 30 by 5: seq(0, 30, 5)). Static numbers can cut-off your data if the data changes!.
https://rdrr.io/r/base/strptime.html —– see all % shortcuts
Below is a demonstration of some plots where the bins and the plot labels/gridlines are aligned and not aligned:
Click “Code” to see the code
# 7-day binwidth defaults
#################
ggplot(central_data, aes(x = date_onset)) + # x column must be class Date
geom_histogram(
binwidth = 7, # 7 days per bin (! starts at first case!)
color = "darkblue", # color of lines around bars
fill = "lightblue") + # color of bar fill
labs(
title = "MISALIGNED",
subtitle = "!CAUTION: 7-day bars start Thursdays with first case\ndefault axis labels/ticks not aligned")
# 7-day bins + Monday labels
#############################
ggplot(central_data, aes(x = date_onset)) +
geom_histogram(
binwidth = 7, # 7-day bins with start at first case
color = "darkblue",
fill = "lightblue") +
scale_x_date(
expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "3 weeks", # Monday every 3 weeks
date_minor_breaks = "week", # Monday weeks
date_labels = "%d\n%b\n'%y")+ # label format
scale_y_continuous(
expand = c(0,0))+ # remove excess space under x-axis, make flush with labels
labs(
title = "MISALIGNED",
subtitle = "!CAUTION: 7-day bars start Thursdays with first case\nDate labels and gridlines on Mondays")
# 7-day bins + Months
#####################
ggplot(central_data, aes(x = date_onset)) +
geom_histogram(
binwidth = 7,
color = "darkblue",
fill = "lightblue") +
scale_x_date(
expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "months", # 1st of month
date_minor_breaks = "week", # Monday weeks
date_labels = "%d\n%b\n'%y")+ # label format
scale_y_continuous(
expand = c(0,0))+ # remove excess space under x-axis, make flush with labels
labs(
title = "MISALIGNED",
subtitle = "!CAUTION: 7-day bars start Thursdays with first case\nGridlines at 1st of each month (with labels) and weekly on Mondays\nLabels on 1st of each month")
# TOTAL MONDAY ALIGNMENT: specify manual bin breaks to be mondays
#################################################################
ggplot(central_data, aes(x = date_onset)) +
geom_histogram(
# histogram breaks set to 7 days beginning Monday before first case
breaks = seq.Date(
from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 1)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 1)),
by = "7 days"),
color = "darkblue",
fill = "lightblue") +
scale_x_date(
expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "3 weeks", # Monday every 3 weeks
date_minor_breaks = "week", # Monday weeks
date_labels = "%d\n%b\n'%y")+ # label format
labs(
title = "ALIGNED Mondays",
subtitle = "7-day bins manually set to begin Monday before first case (28 Apr)\nDate labels and gridlines on Mondays as well")
# TOTAL SUNDAY ALIGNMENT: specify manual bin breaks AND labels to be Sundays
############################################################################
ggplot(central_data, aes(x = date_onset)) +
geom_histogram(
# histogram breaks set to 7 days beginning Sunday before first case
breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 7)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 7)),
by = "7 days"),
color = "darkblue",
fill = "lightblue") +
scale_x_date(
expand = c(0,0),
# date label breaks set to every 3 weeks beginning Sunday before first case
breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 7)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 7)),
by = "3 weeks"),
# gridlines set to weekly beginning Sunday before first case
minor_breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 7)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 7)),
by = "7 days"),
date_labels = "%d\n%b\n'%y")+ # label format
labs(title = "ALIGNED Sundays",
subtitle = "7-day bins manually set to begin Sunday before first case (27 Apr)\nDate labels and gridlines manually set to Sundays as well")
# Check values of bars by creating dataframe of grouped values
# central_tab <- central_data %>%
# mutate(week = aweek::date2week(date_onset, floor_day = TRUE, factor = TRUE)) %>%
# group_by(week, .drop=F) %>%
# summarize(n = n()) %>%
# mutate(groups_3wk = 1:(nrow(central_tab)+1) %/% 3) %>%
# group_by(groups_3wk) %>%
# summarize(n = n())Designate a column containing groups
In any of the code template (Sunday weeks, Monday weeks), make the following changes:
aes() within the geom_histogram() (don’t forget comma afterward)aes(), provide the grouping column name to group = and fill = (no quotes needed). group is necessary, while fill changes the color of the bar.fill = argument outside of the aes(), as it will override the one insideaes() will apply by group, whereas any outside will apply to all bars (e.g. you may want color = outside, so each bar has the same color perimeter/border)geom_histogram(
aes(group = gender, fill = gender))
Adjust colors:
scale_fill_manual() (note scale_color_manual() is different!).
values = argument to apply a vector of colors.na.value = to specify a color for missing values.labels = argument in scale_fill_manual() change the legend text labels - it is easy to accidentally give labels in the incorrect order and have an incorrect legend! It is recommended to instead convert the group column to class Factor and designate factor labels and order, as explained below.Adjust the stacking order and Legend
Stacking order, and the labels for each group in the legend, is best adjusted by classifying the group column as class Factor. You can then designate the levels and their labels, and the order (which is reflected in stack order).
Step 1: Before making the ggplot, convert the group column to class Factor using factor() from base R.
For example, with a column “gender” with values “m” and “f” and NA, this can be put in a mutate() command as:
dataset <- dataset %>%
mutate(gender = factor(gender,
levels = c(NA, "f", "m"),
labels = c("Missing", "Female", "Male"),
exclude = NULL))
The above code establishes the levels, in the ordering that missing values are “first” (and will appear on top). Then the labels that will show are given in the same order. Lastly, the exclude statement ensures that NA is included in the ordering (by default factor() ignores NA).
Read more about factors in their dedicated handbook page (LINK).
Adjusting the legend
Read more about legends in the ggplot tips page. Here are a few highlights:
theme(legend.position = "top") (or “bottom”, “left”, “right”)theme(legend.direction = "horizontal")theme(legend.title = element_blank()) to have no titleSee the page of ggplot() tips for more details on legends.
These steps are shown in the example below:
Click “Code” to see the code
########################
# bin break points for histogram defined here for clarity
# starts the Monday before first case, end Monday after last case
bin_breaks = seq.Date(
from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 1)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 1)),
by = "7 days") # bins are 7-days
# Set gender as factor and missing values as first level (to show on top)
central_data <- linelist %>%
filter(hospital == "Central Hospital") %>%
mutate(gender = factor(
gender,
levels = c(NA, "f", "m"),
labels = c("Missing", "Female", "Male"),
exclude = NULL))
# make plot
###########
ggplot(central_data, aes(x = date_onset)) +
geom_histogram(
aes(group = gender, fill = gender), # arguments inside aes() apply by group
color = "black", # arguments outside aes() apply to all data
breaks = bin_breaks)+ # see breaks defined above
# The labels on the x-axis
scale_x_date(
expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "3 weeks", # labels appear every 3 Monday weeks
date_minor_breaks = "week", # vertical lines appear every Monday week
date_labels = "%d\n%b\n'%y")+ # date labels format
# y-axis
scale_y_continuous(
expand = c(0,0))+ # removes excess y-axis space between bottom of bars and the labels
#scale of colors and legend labels
scale_fill_manual(
values = c("grey", "orange", "purple"))+ # specify fill colors ("values") - attention to order!
# aesthetic themes
theme_minimal()+ # a set of themes to simplify plot
theme(
plot.caption = element_text(face = "italic", hjust = 0), # caption on left side in italics
axis.title = element_text(face = "bold"))+ # axis titles in bold
# labels
labs(
title = "Weekly incidence of cases, by gender",
subtitle = "Subtitle",
fill = "Gender", # provide new title for legend
x = "Week of symptom onset",
y = "Weekly incident cases reported",
caption = stringr::str_glue("n = {nrow(central_data)} from Central Hospital; Case onsets range from {format(min(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')} to {format(max(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')}\n{nrow(central_data %>% filter(is.na(date_onset)))} cases missing date of onset and not shown"))Display bars side-by-side
Side-by-side display of group bars (as opposed to stacked) is specified within geom_histogram() with position = "dodge".
If there are more than two value groups, these can become difficult to read. Consider instead using a faceted plot (small multiples) (see tab). To improve readability in this example, missing gender values are removed.
Click “Code” to see the code
########################
# bin break points for histogram defined here for clarity
# starts the Monday before first case, end Monday after last case
bin_breaks = seq.Date(from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 1)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 1)),
by = "7 days") # bins are 7-days
# New dataset without rows missing gender
central_data_dodge <- linelist %>%
filter(hospital == "Central Hospital") %>%
filter(!is.na(gender)) %>% # remove rows missing gender
mutate(gender = factor(gender, # factor now has only two levels (missing not included)
levels = c("f", "m"),
labels = c("Female", "Male")))
# make plot
###########
ggplot(central_data_dodge, aes(x = date_onset)) +
geom_histogram(
aes(group = gender, fill = gender), # arguments inside aes() apply by group
color = "black", # arguments outside aes() apply to all data
breaks = bin_breaks,
position = "dodge")+ # see breaks defined above
# The labels on the x-axis
scale_x_date(expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "3 weeks", # labels appear every 3 Monday weeks
date_minor_breaks = "week", # vertical lines appear every Monday week
date_labels = "%d\n%b\n'%y")+ # date labels format
# y-axis
scale_y_continuous(expand = c(0,0))+ # removes excess y-axis space between bottom of bars and the labels
#scale of colors and legend labels
scale_fill_manual(values = c("pink", "lightblue"))+ # specify fill colors ("values") - attention to order!
# aesthetic themes
theme_minimal()+ # a set of themes to simplify plot
theme(plot.caption = element_text(face = "italic", hjust = 0), # caption on left side in italics
axis.title = element_text(face = "bold"))+ # axis titles in bold
# labels
labs(title = "Weekly incidence of cases, by gender",
subtitle = "Subtitle",
fill = "Gender", # provide new title for legend
x = "Week of symptom onset",
y = "Weekly incident cases reported",
caption = stringr::str_glue("n = {nrow(central_data)} from Central Hospital; Case onsets range from {format(min(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')} to {format(max(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')}\n{nrow(central_data %>% filter(is.na(date_onset)))} cases missing date of onset and not shown"))As with other ggplots, you can create facetted plots (“small multiples”) off values in a column. As explained in the ggplot tips page of this handbook, you can use either:
facet_wrap()facet_grid()For epicurves, facet_wrap() is typically easiest as it is likely that you only need to facet on one column. The general syntax is facet_wrap(rows ~ cols), where to the left of the tilde (~) is the name of a column to be spread across the “rows” of the new plot, and to the right of the tilde is the name of a column to be spread across the “columns” of the new plot.
Most simply, just use one column name, to the right of the tilde: facet_wrap(~age_cat).
Free axes
You will need to decide whether the scales (scales =) of the axes for each facet are “fixed” to the same dimensions (default), or “free” (meaning they will change based on the data within the facet). You can also specify “free_x” or “free_y” to release in only one dimension.
Number of cols and rows
This can be specified with ncol = and nrow = within facet_wrap().
Order of panels
To change the order of appearance, change the underlying order of the levels of the factor column used to create the facets.
Aesthetics
Font size and face, strip color, etc. can be modified through theme() with arguments like:
strip.text = element_text() (size, colour, face, angle…)strip.background = element_rect() (e.g. element_rect(fill=“red”))The position of the strip can be modified as the strip.position = argument within facet_wrap() (e.g. “bottom”, “top”, “left”, “right”)
Strip labels
Labels of the facet plots can be modified through the “labels” of the column as a factor, or by the use of a “labeller”.
Make a labeller like this, using the function as_labeller() from ggplot2:
my_labels <- as_labeller(c(
"0-4" = "Ages 0-4",
"5-9" = "Ages 5-9",
"10-14" = "Ages 10-14",
"15-19" = "Ages 15-19",
"20-29" = "Ages 20-29",
"30-49" = "Ages 30-49",
"50-69" = "Ages 50-69",
"70+" = "Over age 70"))An example plot
Faceted by column age_cat. Click “Code” to see the code.
# make plot
###########
ggplot(central_data, aes(x = date_onset)) +
geom_histogram(
aes(group = age_cat, fill = age_cat), # arguments inside aes() apply by group
color = "black", # arguments outside aes() apply to all data
breaks = bin_breaks)+ # see breaks defined above
# The labels on the x-axis
scale_x_date(expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "2 months", # labels appear every 2 months
date_minor_breaks = "1 month", # vertical lines appear every 1 month
date_labels = "%b\n'%y")+ # date labels format
# y-axis
scale_y_continuous(expand = c(0,0))+ # removes excess y-axis space between bottom of bars and the labels
# aesthetic themes
theme_minimal()+ # a set of themes to simplify plot
theme(plot.caption = element_text(face = "italic", hjust = 0), # caption on left side in italics
axis.title = element_text(face = "bold"),
legend.position = "bottom",
strip.text = element_text(face = "bold", size = 10),
strip.background = element_rect(fill = "grey"))+ # axis titles in bold
# create facets
facet_wrap(~age_cat,
ncol = 4,
strip.position = "top",
labeller = my_labels)+
# labels
labs(title = "Weekly incidence of cases, by age category",
subtitle = "Subtitle",
fill = "Age category", # provide new title for legend
x = "Week of symptom onset",
y = "Weekly incident cases reported",
caption = stringr::str_glue("n = {nrow(central_data)} from Central Hospital; Case onsets range from {format(min(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')} to {format(max(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')}\n{nrow(central_data %>% filter(is.na(date_onset)))} cases missing date of onset and not shown"))See this link for more information on labellers.
Add total epidemic to background
Add a separate geom_histogram() command before the current one. Specify that the data used is the data without the column used for faceting (see select()). Then, specify a color like “grey” and a degree of transparency to make it appear in the background.
geom_histogram(data = select(central_data, -age_cat), color = "grey", alpha = 0.5)+
Note that the y-axis maximum is now based on the height of the entire epidemic. Click “Code” to see the code.
ggplot(central_data, aes(x = date_onset)) +
# for background shadow of whole outbreak
geom_histogram(data = select(central_data, -age_cat), color = "grey", alpha = 0.5)+
# actual epicurves by group
geom_histogram(
aes(group = age_cat, fill = age_cat), # arguments inside aes() apply by group
color = "black", # arguments outside aes() apply to all data
breaks = bin_breaks)+ # see breaks defined above
# Labels on x-axis
scale_x_date(expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "2 months", # labels appear every 2 months
date_minor_breaks = "1 month", # vertical lines appear every 1 month
date_labels = "%b\n'%y")+ # date labels format
# y-axis
scale_y_continuous(expand = c(0,0))+ # removes excess y-axis space between bottom of bars and the labels
# aesthetic themes
theme_minimal()+ # a set of themes to simplify plot
theme(plot.caption = element_text(face = "italic", hjust = 0), # caption on left side in italics
axis.title = element_text(face = "bold"),
legend.position = "bottom",
strip.text = element_text(face = "bold", size = 10),
strip.background = element_rect(fill = "white"))+ # axis titles in bold
# create facets
facet_wrap(~age_cat, # each plot is one value of age_cat
ncol = 4, # number of columns
strip.position = "top", # position of the facet title/strip
labeller = my_labels)+ # labeller defines above
# labels
labs(title = "Weekly incidence of cases, by age category",
subtitle = "Subtitle",
fill = "Age category", # provide new title for legend
x = "Week of symptom onset",
y = "Weekly incident cases reported",
caption = stringr::str_glue("n = {nrow(central_data)} from Central Hospital; Case onsets range from {format(min(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')} to {format(max(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')}\n{nrow(central_data %>% filter(is.na(date_onset)))} cases missing date of onset and not shown"))Create one facet with ALL data
To do this, you duplicate all the data (double the number of rows in the dataset) and in the faceted column have a new value (e.g. “all”) which indicates all the duplicated rows. A helped function is below that enables this:
# Define helper function
CreateAllFacet <- function(df, col){
df$facet <- df[[col]]
temp <- df
temp$facet <- "all"
merged <-rbind(temp, df)
# ensure the facet value is a factor
merged[[col]] <- as.factor(merged[[col]])
return(merged)
}
# Create dataset that is duplicated, to show "all zones" as another facet level
central_data2 <- CreateAllFacet(central_data, col = "age_cat") %>%
mutate(facet = factor(facet,
levels = c("all", "0-4", "5-9", "10-14", "15-19", "20-29", "30-49", "50-69", "70+")))
# check
table(central_data2$facet, useNA = "always")
##
## all 0-4 5-9 10-14 15-19 20-29 30-49 50-69 70+ <NA>
## 454 82 83 88 62 78 51 3 0 7Notable changes to the ggplot command are:
facet_wrap(facet~.), and ncol = 1You may also need to adjust the width and height of the save plot image (see ggsave()).
ggplot(central_data2, aes(x = date_onset)) +
# actual epicurves by group
geom_histogram(
aes(group = age_cat, fill = age_cat), # arguments inside aes() apply by group
color = "black", # arguments outside aes() apply to all data
breaks = bin_breaks)+ # see breaks defined above
# Labels on x-axis
scale_x_date(expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "2 months", # labels appear every 2 months
date_minor_breaks = "1 month", # vertical lines appear every 1 month
date_labels = "%b\n'%y")+ # date labels format
# y-axis
scale_y_continuous(expand = c(0,0))+ # removes excess y-axis space between bottom of bars and the labels
# aesthetic themes
theme_minimal()+ # a set of themes to simplify plot
theme(plot.caption = element_text(face = "italic", hjust = 0), # caption on left side in italics
axis.title = element_text(face = "bold"),
legend.position = "bottom")+
# create facets
facet_wrap(facet~. , # each plot is one value of facet
ncol = 1)+
# labels
labs(title = "Weekly incidence of cases, by age category",
subtitle = "Subtitle",
fill = "Age category", # provide new title for legend
x = "Week of symptom onset",
y = "Weekly incident cases reported",
caption = stringr::str_glue("n = {nrow(central_data)} from Central Hospital; Case onsets range from {format(min(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')} to {format(max(central_data$date_onset, na.rm=T), format = '%a %d %b %Y')}\n{nrow(central_data %>% filter(is.na(date_onset)))} cases missing date of onset and not shown"))Add a moving averages to a ggplot() epicurve in one of two ways:
geom_line())ggplot() commandIn this approach, the moving average is calculated in the dataset prior to plotting:
mutate(), a new column is created to hold the average. slide_index() from slider package is used as shown below.ggplot(), a geom_line() is added after the histogram, reflecting the moving average.See the helpful online vignette for the slider package
pacman::p_load(slider) # slider used to calculate rolling averages
# make dataset of daily counts and 7-day moving average
#######################################################
ll_counts_7day <- linelist %>%
## count cases by date
count(date_onset,
name = "new_cases") %>% # name of new column
## calculate the average number of cases in the preceding 7 days
mutate(
avg_7day = slider::slide_index( # create new column
new_cases, # calculate based on value in new_cases column
.i = date_onset, # index is date_onset col, so non-present dates are included in window
.f = ~mean(.x, na.rm = TRUE), # function is mean() with missing values removed
.before = 6, # window is the day and 6-days before
.complete = FALSE), # must be FALSE for unlist() to work in next step
avg_7day = unlist(avg_7day))
# plot
######
ggplot(data = ll_counts_7day, aes(x = date_onset)) +
geom_histogram(aes(y = new_cases),
fill="#92a8d1",
stat = "identity",
position = "stack",
colour = "#92a8d1")+
geom_line(aes(y = avg_7day, lty = "7-day \nrolling avg"),
color="red",
size = 1) +
scale_x_date(date_breaks = "1 month",
date_labels = '%d/%m',
expand = c(0,0)) +
scale_y_continuous(expand = c(0,0),
limits = c(0, NA)) +
labs(x="",
y ="Number of confirmed cases",
fill = "Legend")+
theme_minimal()+
theme(legend.title = element_blank()) # removes title of legendUsing the tidyquant package to calculate the moving average on-the-fly (within ggplot()).
This option is more difficult to modify than pre-calculating the moving average. By default,geom_ma() uses the Simple Moving Average (SMA) (TRR::SMA()). See documentation by entering ?SMA in your R console. Calculates the arithmatic mean over the past n observations. Also note how the moving average does not begin as early as the previous example.
library(tidyquant)
# make daily count data
#######################
ll_counts_7day <- linelist %>%
count(date_onset, name = "daily_cases")
# plot
######
ggplot(data = ll_counts_7day, # use daily count data
aes(x = date_onset, # date x-axis
y = daily_cases))+ # counts
# histogram in the background
geom_histogram(stat = "identity", # height = value in the cell, not number of rows
color = "#92a8d1", # color of lines within histogram
fill = "#92a8d1")+ # color of histogram
# moving average line
tidyquant::geom_ma(n = 7, # window width
size = 2, # line size
color = "black", # line color
lty = "solid" # line type ()
)+
# labels for x-axis
scale_x_date(date_breaks = "2 months", # labels every 2 months
date_minor_breaks = "1 month", # gridlines every month
date_labels = '%b\n%Y')+ #labeled by month with year below
# Choose color palette (uses RColorBrewer package)
scale_fill_brewer(palette = "Pastel2")+
theme_minimal()+
labs(x = "Date of onset",
y = "Daily case incidence",
title = "Daily case incidence, with 7-day moving average")The most recent data shown in epicurves should often be marked as tentative, or subject to reporting delays. This can be done in by adding a vertical line and/or rectangle over a specified number of days. Here are two options:
annotate():annotate(geom = "segment"). Provide x, xend, y, and yend. Adjust size, linetype (lty), and color.annotate(geom = "rect"). Provide xmin/xmax/ymin/ymax. Adjust color and alpha.geom_segment() and geom_rect():annotate()CAUTION: While you can use geom_rect() to draw a rectangle, adjusting the transparency (alpha) does not work in a linelist context. This function overlays a rectangle for each observation/row!. Try a very low alpha (e.g. 0.01), or use annotate(geom = "rect") as shown.
annotate()annotate(geom = "rect"), the xmin and xmax arguments must be given inputs of class Date.annotate() online exampleggplot(central_data, aes(x = date_onset)) +
# histogram
geom_histogram(
breaks = seq.Date(
from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 1)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 1)),
by = "7 days"),
color = "darkblue",
fill = "lightblue") +
# scales
scale_y_continuous(expand = c(0,0))+
scale_x_date(
expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "1 month", # 1st of month
date_minor_breaks = "1 month", # 1st of month
date_labels = "%b\n'%y")+ # label format
# labels and theme
labs(title = "Using annotate()\nRectangle and line showing that data from last 21-days are tentative",
x = "Week of symptom onset",
y = "Weekly case indicence")+
theme_minimal()+
# add semi-transparent red rectangle to tentative data
annotate("rect",
xmin = as.Date(max(central_data$date_onset, na.rm = T) - 21), # note must be wrapped in as.Date()
xmax = as.Date(Inf), # note must be wrapped in as.Date()
ymin = 0,
ymax = Inf,
alpha = 0.2, # alpha easy and intuitive to adjust using annotate()
fill = "red")+
# add black vertical line on top of other layers
annotate("segment",
x = max(central_data$date_onset, na.rm = T) - 21, # 21 days before last data
xend = max(central_data$date_onset, na.rm = T) - 21,
y = 0, # line begins at y = 0
yend = Inf, # line to top of plot
size = 2, # line size
color = "black",
lty = "solid")+ # linetype e.g. "solid", "dashed"
# add text in rectangle
annotate("text",
x = max(central_data$date_onset, na.rm = T) - 15,
y = 20,
label = "Subject to reporting delays",
angle = 90)The same black vertical line can be achieved with the code below, but using geom_vline() you lose the ability to control the height:
geom_vline(xintercept = max(central_data$date_onset, na.rm = T) - 21,
size = 2,
color = "black")
geom_segment() and geom_rect()ggplot(central_data, aes(x = date_onset)) +
# histogram
geom_histogram(
breaks = seq.Date(
from = as.Date(floor_date(min(central_data$date_onset, na.rm=T), "week", week_start = 1)),
to = as.Date(ceiling_date(max(central_data$date_onset, na.rm=T), "week", week_start = 1)),
by = "7 days"),
color = "darkblue",
fill = "lightblue") +
# scales
scale_y_continuous(expand = c(0,0))+
scale_x_date(
expand = c(0,0), # remove excess x-axis space below and after case bars
date_breaks = "3 weeks", # Monday every 3 weeks
date_minor_breaks = "week", # Monday weeks
date_labels = "%d\n%b\n'%y")+ # label format
# labels and theme
labs(title = "Using geom_segment() and geom_rect()\nRectangle and line showing that data from last 21-days are tentative",
subtitle = "")+
theme_minimal()+
# make rectangle covering last 21 days
geom_rect(aes(
xmin = as.Date(max(central_data$date_onset, na.rm = T) - 21), # note must be wrapped in as.Date()
xmax = as.Date(Inf), # note must be wrapped in as.Date()
ymin = 0,
ymax = Inf,
color = "Reporting delays\npossible"), # sets label for legend (note: is within aes())
alpha = .002, # !!! Difficult to adjust transparency with this option
fill = "red")+
# make vertical line
geom_segment(aes(x = max(central_data$date_onset, na.rm = T) - 21,
xend = max(central_data$date_onset, na.rm = T) - 21,
y = 0,
yend = Inf),
color = "black",
lty = "solid",
size = 2)+
theme(legend.title = element_blank()) # remove title of legendTwo axes
Here is an option if you want multi-level date labels, without duplicating the lower label levels (e.g. for year or month).
Remember, you can can use tools like \n within the date_labels or labels arguments to put parts of each label on a new line below. However, the code below helps you take years or months (for example) on a lower line and only once.
A few notes on the code below:
# Create dataset of case counts by week
#######################################
central_weekly <- linelist %>%
filter(hospital == "Central Hospital") %>% # filter linelist
mutate(week = aweek::date2week(date_onset, # create weeks column
floor_day = T, # round dates to weeks
factor = T)) %>% # include weeks with no cases
count(week, .drop=F) %>% # summarize weekly case counts
mutate(week_as_date = week2date(week, # make col of week start date in Date class (for plotting)
week_start = "Monday")) %>% # this is done after count() so that all weeks are present
ungroup()
# plot
######
ggplot(central_weekly) +
geom_line(aes(x = week_as_date, y = n), # make line, specify x and y
stat = "identity") + # because line height is count number
scale_x_date(date_labels="%b", # date label format show month
date_breaks="month", # date labels on 1st of each month
expand=c(0,0)) + # remove excess space
facet_grid(~lubridate::year(week_as_date), # facet on year (of Date class column)
space="free_x",
scales="free_x", # x-axes adapt to data range (not "fixed")
switch="x") + # facet labels (year) on bottom
theme_bw() +
theme(strip.placement = "outside", # facet labels placement
strip.background = element_rect(fill = NA, # facet labels no fill grey border
colour = "grey50"),
panel.spacing = unit(0, "cm"))+ # no space between facet panels
labs(title = "Nested year labels, grey label border")
# plot no border
################
ggplot(central_weekly,
aes(x = week_as_date, y = n)) + # establish x and y for entire plot
geom_line(stat = "identity", # make line, line height is count number
color = "#69b3a2") + # line color
geom_point(size=1, color="#69b3a2") + # make points at the weekly data points
geom_area(fill = "#69b3a2", # fill area below line
alpha = 0.4)+ # fill transparency
scale_x_date(date_labels="%b", # date label format show month
date_breaks="month", # date labels on 1st of each month
expand=c(0,0)) + # remove excess space
facet_grid(~lubridate::year(week_as_date), # facet on year (of Date class column)
space="free_x",
scales="free_x", # x-axes adapt to data range (not "fixed")
switch="x") + # facet labels (year) on bottom
theme_bw() +
theme(strip.placement = "outside", # facet label placement
strip.background = element_blank(), # no facet lable background
panel.grid.minor.x = element_blank(),
panel.border = element_rect(colour="grey40"), # grey border to facet PANEL
panel.spacing=unit(0,"cm"))+ # No space between facet panels
labs(title = "Nested year labels - points, shaded, no label border") The above techniques were adapted from this and this post on stackoverflow.com.
To learn generally how to group and aggregate data, see the handbook page on Grouping/Aggregating.
In this circumstance, we demonstrate aggregating into weeks, months, and days.
Create a new column that is weeks, then use group_by() with summarize() to get weekly case counts.
To aggregate into weeks and show ALL weeks (even ones with no cases), do this:
mutate(), using date2week() from the aweek package:
week_start = to set the weekday start of the week (e.g. “Monday”, “Sunday”) - the default is Mondayfloor_date = TRUE so the output is “YYYY-Www” (not the default “YYYY-Www-d” including day of the week)factor = TRUE so all possible weeks in the range are included as factor levels, so all weeks will appear when summarized.numeric = TRUE if you want only the week number (note this will not distinguish between years).drop = F in the subsequent group_by() command, preserving all weeks even those with no cases.For example:
# Make dataset of weekly case counts
weekly_counts <- linelist %>%
mutate(week = aweek::date2week(date_onset, # new column of week of onset
floor_day = T, # show as weeks without weekday
factor = TRUE)) %>% # include all possible weeks
group_by(week, .drop = F) %>% # group data by new week col, keep all weeks
summarise(n_weekly_cases = n()) %>% # new summary column = number of linelist rows in each week
ungroup() # deactivate grouping
# Optional: add column of start DATE for each week
# Use this in ggplot() when class Date x-axis is expected
# note: add this AFTER the summarize statement above, to ensure all weeks are present
weekly_counts <- weekly_counts %>%
mutate(week_as_date = aweek::week2date(week, week_start = "Monday")) # output is Monday date of each weekHere are the first 50 rows of the resulting dataframe:
Alternatively, you can use the lubridate** package’s floor_date() function**. Set unit = "week and you can adjust week_start = to set the start day (7 = Sunday, 1 = Monday). This approach will not create a factor Class column so weeks with no cases may not appear. If this is needed, add the complete() command as described in the Months section below.
To aggregate cases into months, the process is very similar but instead of the aweek package, use floor_date() from the lubridate package, with the argument unit = "months". This rounds each date down to the 1st of its month. The output will be class Date.
Unlike the aweek example, all months are not included. To ensure all months are included, even if no cases were reported, add the complete() command at the end, to enforce that the new month column is a complete sequence of all months in the possible range.
# Make dataset of weekly case counts
monthly_counts <- linelist %>%
mutate(month = lubridate::floor_date(date_onset, unit = "months")) %>% # new column, 1st of month of onset
group_by(month) %>% # group data by new month col, keep all months
summarise(n_monthly_cases = n()) %>% # new summary column = number of linelist rows in each month
complete(month = seq.Date(min(month), # fill-in all months with no cases reported
max(month),
by="month")) To aggregate a linelist into days, use the same approach but there is no need to create a new column. Use group_by() on the date column (e.g. date_onset).
If plotting a histogram, missing days in the data are not a problem as long as the column is class Date. However, it may be important for other types of plots or tables to have all possible days apear in the data. This is done with: tidyr::complete()
# Make dataset of weekly case counts
daily_counts <- linelist %>%
group_by(date_onset, .drop = F) %>% # group data by new month col, keep all months
summarise(n_monthly_cases = n()) %>% # new summary column = number of linelist rows in each day
complete(date_onset = seq.Date(min(date_onset), # ensure all days appear
max(date_onset),
by="day")) Often instead of a linelist, you begin with aggregated counts from facilities, districts, etc. You can make an epicurve from ggplot() but the code will be slightly different. The incidence package does not support aggregate data.
This section will utilize the counts_data dataset that was imported earlier, in the data preparation section. It is the linelist aggregated to day-hospital counts. The first 50 rows are displayed below.
As before, we must ensure date variables are correctly classified.
We can plot a daily epicurve from these data. Here are the differences:
y = to the counts column within the primary aesthetics aes()stat = "identity" within geom_histogram() indicates that the y-values could be counts from the y = column in aes()ggplot(data = count_data, aes(x = as.Date(date_hospitalisation), y = n_cases))+
geom_histogram(stat = "identity")+
labs(x = "Week of report",
y = "Number of cases",
Title = "Daily case incidence, from daily count data")aggregate further
To aggregated further, into weeks, we use the package aweek and function date2week():
# Create weekly dataset with epiweek column
count_data_weekly <- count_data %>%
mutate(epiweek = aweek::date2week(count_data$date_hospitalisation,
week_start = "Monday", # epiweeks to begin on Mondays
floor_day = TRUE, # round each day into the epiweek - don't show week-day
factor = TRUE)) %>% # make an ordered factor and expand data to include all possible weeks
group_by(hospital, epiweek, .drop=F) %>%
summarize(n_cases_weekly = sum(n_cases, na.rm=T)) The first 50 rows of count_data are displayed below.
For the plotting we also specify the factor level order of hospital.
count_data_weekly <- count_data_weekly %>%
mutate(hospital = factor(hospital,
levels = c("Missing", "Port Hospital", "Military Hospital", "Central Hospital", "St. Mark's Maternity Hospital (SMMH)", "Other")))Now plot by epiweek. In this case we want the epiweeks to be recognized as class Date so that we can use scale_x_date() to easily adjust the x-axis labels. So within aes() we set x = to epiweek wrapped in the aweek function week2date() which transforms the week back to a date (given a particular start day of the week).
ggplot(data = count_data_weekly,
aes(x = week2date(epiweek, week_start = "Monday"),
y = n_cases_weekly,
group = hospital,
fill = hospital))+
geom_histogram(stat = "identity")+
# labels for x-axis
scale_x_date(date_breaks = "2 months", # labels every 2 months
date_minor_breaks = "1 month", # gridlines every month
date_labels = '%b\n%Y')+ #labeled by month with year below
# Choose color palette (uses RColorBrewer package)
scale_fill_brewer(palette = "Pastel2")+
theme_minimal()+
labs(x = "Week of onset",
y = "Weekly case incidence",
fill = "Hospital",
title = "Weekly case incidence, from aggregated count data by hospital")Although there are fierce discussions about the validity of this within the data visualization community, many supervisors want to see an epicurve or similar chart with a percent overlaid with a second axis.
In ggplot it is difficult to do this, except for the case where you are showing a line reflecting the proportion of a category shown in the bars below.
See the handbook page on ggplot tips for details on how to make a second axis.
https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales
# Value used to transform the data
coeff <- 10
# A few constants
temperatureColor <- "#69b3a2"
priceColor <- rgb(0.2, 0.6, 0.9, 1)
# dataset
plot_data <- linelist %>%
mutate(week = aweek::date2week(date_onset, floor_day = T, factor = T)) %>%
group_by(week) %>%
summarize(n_cases = n(),
n_male = sum(gender == "m"),
pct_male = round(100*(n_male / n()))) %>%
mutate(week_as_date = week2date(week))
# complete(date_onset = seq.Date(min(date_onset),
# max(date_onset),
# by = "day"))
total_value <- sum(plot_data$n_male, na.rm = T)
ggplot(data = plot_data, aes(x = week_as_date))+
geom_bar(aes(y = n_cases), stat = "identity")+
geom_line(aes(y = n_male / 0.01), size=2, color="blue")+
#scale_y_continuous(sec.axis = sec_axis(~.*0.01, name = "Percentage"))
scale_y_continuous(sec.axis = sec_axis(~(./total_value)*100, name = "Percentages"))ggplot(linelist, aes(x=date_onset)) +
geom_line( aes(y=temperature), size=2, color=temperatureColor) + geom_line( aes(y=price / coeff), size=2, color=priceColor) +
scale_y_continuous(
# Features of the first axis
name = "Temperature (Celsius °)",
# Add a second axis and specify its features
sec.axis = sec_axis(~.*coeff, name="Price ($)")
) +
theme_ipsum() +
theme( axis.title.y = element_text(color = temperatureColor, size=13), axis.title.y.right = element_text(color = priceColor, size=13) ) +
ggtitle(“Temperature down, price up”)
```r
library(reshape2)
# group the data by week, summarize counts by group (gender)
linelist_week <- linelist %>%
mutate(onset_epiweek = aweek::date2week(date_onset, floor_day = TRUE, factor = TRUE)) %>%
group_by(onset_epiweek, .drop = F) %>%
summarize(num_male = sum(gender == "m"),
num_female = sum(gender == "f"),
pct_male = round(100*(num_male / n())),
med_age = median(as.numeric(age), na.rm=T)
)
# remove pct and melt
linelist_week_melted <- linelist_week %>%
select(-c("pct_male", "med_age")) %>%
reshape2::melt(id.vars = c("onset_epiweek"))
# merge together (multiple of the same values in week will attach to melted)
linelist_week_melted <- merge(linelist_week_melted,
linelist_week,
by = "onset_epiweek")
second_axis <- ggplot(linelist_week_melted,
aes(x = aweek::week2date(onset_epiweek),
y = value, group = variable,
fill = variable)) +
# bars
geom_bar(stat = "identity")+
# Colors and labels of confirmed/probable
scale_fill_manual(values = c("blue", "red"),
labels = str_to_sentence(levels(factor(linelist_week_melted$variable)))) +
geom_line(mapping = aes(y = pct_male, color = "% male"), size = 0.5) +
scale_color_manual(values = "black")+
scale_y_continuous(sec.axis = sec_axis(~(./sum(linelist_week_melted$value, na.rm = T)*100), name = "name here", breaks = seq(0, 100, 20)))+
# X-axis scale labels (not aggregation, just the labels)
scale_x_date(# Sets date label breaks as every week
breaks = function(x) seq.Date(from = min(linelist$date_onset, na.rm = T), to = max(linelist$date_onset, na.rm = T), by = "1 week"),
limits = c((min(linelist$date_onset, na.rm=T)), (max(linelist$date_onset, na.rm = T))), # axis limits determined by max/min + buffer
date_labels = "%d-%b", # displays as date # then abbreviated month (e.g. 12 Oct)
expand = c(0, 0)) + # sets origin at (0,0)
# Y-scale in breaks, up to the ymax previously defined
scale_y_continuous(breaks = seq(0, 500, 5), expand=c(0, 0)) +
# Themes for axes, titles, background, etc.
theme(plot.title = element_text(size=20, hjust=0.5, face="bold"),
axis.text = element_text(size=12),
axis.title = element_text(size=14, face="bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(angle=90, vjust=0.5, hjust=1)) +
# Legend specifications
theme(legend.title = element_blank(),
legend.justification = c(0, 1),
legend.position = c(0.09, 0.98),
legend.background = element_blank(),
legend.text = element_text(size = 12)) +
guides(fill = guide_legend(reverse = TRUE, override.aes = list(size = 0.2))) +
# Axis and caption labels
labs(x = "Week of illness onset",
y = "Number of Cases") +
# Title
ggtitle("Cases by week of illness onset")
second_axis
# print
print(plot_defined_cats)
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
ggplot2This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
For appropriate plotting of continuous data, e.g. age, clinical measurements, distance, etc.
As usual, R has built-in functions for quick visualisations. You can opt to install additional packages with more functionality - this is often recommended for presentation-ready visualisations. Specifically, you can use:
boxplot() function from the graphics package (installed automatically with base R)ggplot() function from the ggplot2 package, orVisualisations covered here include:
Plots for one continuous variable:
Scatter plots for two continuous variables.
Preparation includes ensuring you have the correct packages, (install.packages("ggplot2") if needed), and ensuring your data is the correct class and format.
Convert character outcomes to numeric as needed:
linelist <- linelist %>%
mutate(age = as.numeric(age))
Plotting one continuous variable
The in-built graphics package comes with the boxplot() function, allowing straight-forward visualisation of a continuous variable for the whole dataset (A below) or within different groups (B and C below). Note how with C, outcome and gender are written as outcome*gender such that the boxplots are for the four combinations of the two columns.
# For total population
graphics::boxplot(linelist$age,
main = "A) One boxplot() for total dataset") # Plot title
# By subgroup
graphics::boxplot(age ~ outcome*gender,
data = linelist, # Here 'data' is specified so no need to write 'linelist$age' in line above.
main = "B) boxplot() by subgroup")
# By crossed subgroups
graphics::boxplot(age ~ outcome*gender,
data = linelist, # Here 'data' is specified so no need to write 'linelist$age' in line above.
main = "C) boxplot() by crossed groups")Some further options with boxplot() shown below are:
# Varying width by sample size
graphics::boxplot(linelist$age ~ linelist$outcome,
varwidth = TRUE, # width varying by sample size
main="A) Proportional boxplot() widths")
# Notched (violin plot), and varying width
boxplot(age ~ outcome,
data=linelist,
notch=TRUE, # notch at median
main="B) Notched boxplot()",
col=(c("gold","darkgreen")),
xlab="Suppliment and Dose")
# Horizontal
boxplot(age ~ outcome,
data=linelist,
horizontal=TRUE, # flip to horizontal
col=(c("gold","darkgreen")),
main="C) Horizontal boxplot()",
xlab="Suppliment and Dose")Plotting two continuous variables
Scatter plots are helpful for visualising the correlation between two continuous variables.
Using base R, they can simple be visualisation with the plot function.
Code syntax
Ggplot has extensive functionality, and the same code syntax can be used for many different plot types.
A basic breakdown of the ggplot code is as follows:
ggplot(data = linelist,
aes(x = col1, y = col2),
fill = "color")+
geom_boxplot()
ggplot() starts off the function. You can specify the data and aesthetics (see next point) within the ggplot bracket, unless you are combining different data sources or plot types into oneaes() stands for ‘aesthetics’, and is where the columns used for the visualisation are specified. For instance aes(x = col1, y = col2) to specify the data used for the x and y values (where y is the continuous variable in these examples).fill specifies the colour of the boxplot areas. One could also write color to specify outline or point colour.geom_XXX specifies what type of plot. Options include:
geom_boxplot() for a boxplotgeom_violin() for a violin plotgeom_jitter() for a jitter plotgeom_point() for a scatter plotFor more see section on ggplot tips).
Plotting one continuous variable
Below is code for creating box plots, for an entire dataset and by sub group. Note that for the subgroup breakdowns, the ‘NA’ values are also removed using dplyr, otherwise ggplot plots the age distribution for ‘NA’ as a separate boxplot.
# A) Simple boxplot of one numeric variable
ggplot(data = linelist, aes(y = age))+ # only y variable given (no x variable)
geom_boxplot()+
ggtitle("A) Simple ggplot() boxplot")
# B) Box plot by group
ggplot(data = linelist %>% filter(!is.na(outcome)),
aes(y = age, # numeric variable
x = outcome)) + # group variable
geom_boxplot(fill = "gold")+ # create the boxplot and specify colour
ggtitle("B) ggplot() boxplot by gender") # main titleBelow is code for creating violin plots (geom_violin) and jitter plots (geom_jitter). One can specify that the ‘fill’ or ’color’is also determined by the data, thereby inserting these options within the aes bracket.
# A) Violin plot by group
ggplot(data = linelist %>% filter(!is.na(outcome)),
aes(y = age, # numeric variable
x = outcome, # group variable
fill = outcome))+ # fill variable (color of boxes)
geom_violin()+ # create the violin plot
ggtitle("A) ggplot() violin plot by gender") # main title
# B) Jitter plot by group
ggplot(data = linelist %>% filter(!is.na(outcome)),
aes(y = age, # numeric variable
x = outcome, # group variable
color = outcome))+ # Color variable
geom_jitter()+ # create the violin plot
ggtitle("B) ggplot() violin plot by gender") # main titleTo examine further subgroups, one can ‘facet’ the graph. This means the plot will be recreased within specified subgroups. One can use:
facet_wrap() - this will recreate the sub-graphs and present them alphabetically (typically, unless stated otherwise). You can invoke certain options to determine the look of the facets, e.g. nrow=1 or ncol=1 to control the number of rows or columns that the faceted plots are arranged within. See plot A below.facet_grid() - this is suited to seeing subgroups for particular combinations of discrete variables. See plot B below.# A) Facet by one variable
ggplot(data = linelist %>% filter(!is.na(gender) & !is.na(outcome)), # filter retains non-missing gender/outcome
aes(y = age, x = outcome, fill=outcome))+
geom_boxplot()+
ggtitle("A) A ggplot() boxplot by gender and outcome")+
facet_wrap(~gender, nrow = 1)
# B) Facet across two variables
ggplot(data = linelist %>% filter(!is.na(gender) & !is.na(outcome)), # filter retains non-missing gender/outcome
aes(y = age))+
geom_boxplot()+
ggtitle("A) A ggplot() boxplot by gender and outcome")+
facet_grid(outcome~gender)To turn the plot horizontal, flip the coordinates with coord_flip.
# By subgroup
ggplot(data = linelist %>% filter(!is.na(gender) & !is.na(outcome)), # filter retains non-missing gender/outcome
aes(y = age, x = outcome, fill=outcome))+
geom_boxplot()+
ggtitle("B) A horizontal ggplot() boxplot by gender and outcome")+
facet_wrap(gender~., ncol=1) +
coord_flip()Plotting two continuous variables
Following similar syntax, geom_point will allow one to plot two continuous variables against eachother in a scatter plot. Here we again use facet_grid to show the interaction between two different discrete variables.
# By subgroup
ggplot(data = linelist %>% filter(!is.na(gender) & !is.na(outcome)), # filter retains non-missing gender/outcome
aes(y = age, x = age))+
geom_point()+
ggtitle("A horizontal ggplot() boxplot by gender and outcome")+
facet_grid(gender~outcome) There is a huge amount of help online, especially with ggplot. see:
This page will cover methods to calculate and visualize moving averages, for:
To see a moving average for an epicurve, see the page on epicurves (LINK)
Load packages
Using the package slider to calculate a moving average in a dataframe, prior to any plotting.
In this approach, the moving average is calculated in the dataset prior to plotting:
mutate(), a new column is created to hold the average. slide_index() from slider package is used as shown below.ggplot(), a geom_line() is added after the histogram, reflecting the moving average.See the helpful online vignette for the slider package
.before = Inf to achieve cumulative averages from the first rowslide() in simple casesslide_index() to designate a date column as an index, so that dates which do not appear in the dataframe are still included in the window
.before, .after TODO.complete TODOFirst we count the number of cases reported each day. Note that count() is appropriate if the data are in a linelist format (one row per case) - if starting with aggregated counts you will need to follow a different approach (e.g. summarize() - see page on Summarizing data).
# make dataset of daily counts and 7-day moving average
#######################################################
ll_counts_7day <- linelist %>%
count(date_onset, name = "new_cases") # count cases by date, new column is named "new_cases"The new dataset now looks like this:
Next, we create a new column that is the 7-day average. We are using the function slide_index() from slider specifically because we recognize that there are missing days in the above dataframe, and they must be accounted for. To do this, we set a our “index” (.i argument) as the columndate_onset. Sincedate_onsetis a column of class Date, the function recognizes and when calculating it counts the days that do not appear in the dataframe. If you were to use another **slider** function likeslide()`, this indexing would not occur.
Also not that the 7-day window, in this example, is achieved with the argument .before = 6. In this way the window is the day and 6 days preceding. If you want the window to be different (centered or following) use .after in conjunction.
## calculate the average number of cases in the preceding 7 days
ll_counts_7day <- ll_counts_7day %>%
mutate(
avg_7day = slider::slide_index_dbl( # create new column
new_cases, # calculate avg based on value in new_cases column
.i = date_onset, # index column is date_onset, so non-present dates are included in 7day window
.f = ~mean(.x, na.rm = TRUE), # function is mean() with missing values removed
.before = 6, # window is the day and 6-days before
.complete = TRUE)) # fills in first days with NAStep 2 is plotting the 7-day average, in this case shown on top of the underlying daily data.
ggplot(data = ll_counts_7day, aes(x = date_onset)) +
geom_histogram(aes(y = new_cases), fill="#92a8d1", stat = "identity", position = "stack", colour = "#92a8d1")+
geom_line(aes(y = avg_7day), color="red", size = 1) +
scale_x_date(
date_breaks = "1 month",
date_labels = '%d/%m',
expand = c(0,0)) +
scale_y_continuous(expand = c(0,0), limits = c(0, NA)) +
labs(x="", y ="Number of confirmed cases")+
theme_minimal() TBD - tidyquant
per_pos_plot_county <- ggplot(data = filter(tests_per_county),
aes(x = DtSpecimenCollect_Final, y = prop_pos))+
geom_line(size = 1, alpha = 0.2)+ # plot raw values
tidyquant::geom_ma(n=7, size = 2)+ # plot moving average
theme_minimal_hgrid()+
coord_cartesian(xlim = c(as.Date("2020-03-15"), Sys.Date()), ylim = c(0, 15))+
labs(title = "COUNTY-WIDE TESTING PERCENT POSITIVE",
subtitle = "Daily and 7-day moving average",
y = "Percent Positive",
x = "Date Specimen Collected")+
theme_text_size+
theme(axis.text = element_text(face = "bold", size = 14),
panel.background = element_rect(fill = "khaki")
)See the helpful online vignette for the slider package
If your use case requires that you “skip over” weekends and even holidays, you might like almanac package.
The primary tool to visualize and analyze transmission chains is the package epicontacts, developed by the folks at RECON.
links <- epicontacts::make_epicontacts(linelist = mers_korea_2015$linelist,
contacts = mers_korea_2015$contacts,
directed = TRUE)
# plot without time
plot(links,
selector = FALSE,
height = 700,
width = 700)And in a transmission tree, with date of onset on the x-axis:
Note: this currently requires installing a development version of epicontacts from github… @ttree
summary(links)
##
## /// Overview //
## // number of unique IDs in linelist: 162
## // number of unique IDs in contacts: 97
## // number of unique IDs in both: 97
## // number of contacts: 98
## // contacts with both cases in linelist: 100 %
##
## /// Degrees of the network //
## // in-degree summary:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 1.0000 0.6049 1.0000 3.0000
##
## // out-degree summary:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 0.0000 0.6049 0.0000 38.0000
##
## // in and out degree summary:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 0.00 1.00 1.21 1.00 39.00
##
## /// Attributes //
## // attributes in linelist:
## age age_class sex place_infect reporting_ctry loc_hosp dt_onset dt_report week_report dt_start_exp dt_end_exp dt_diag outcome dt_death
##
## // attributes in contacts:
## exposure diff_dt_onsetThis tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Endemic corridor analysis Detecting spikes in syndromic/routine surveillance
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
This page will show you two ways to standardize an outcome, such as hospitalizations or mortality, by characteristics such as age and sex.
{#title_tag .tabset .tabset-fade}There are two main ways to standardize: direct and indirect standardization. Let’s say we would like to standardize COVID-19 mortality in London by age and sex. * For direct standardization, you will have to know the number of the at-risk population and number of deaths for each age and sex stratum. One stratum in our example could be females between ages 15-44. * For indirect standardization, you only need to know the total number of deaths and the age- and sex structure in our study population, London. This option is therefore feasible if age- and sex-specific mortality rates or population numbers are not available. Indirect standardization is furthermore preferable in case of small numbers per stratum, as estimates in direct standardization would be influenced by substantial sampling variation.
To show how indirect standardization is done, we will use the country_demographics dataset which includes population numbers by age (in 5 year categories) and sex (female, male). We will add our own fictitious mortality data to this dataset. To make the dataset ready for use, we will perform the following steps:
Alternatively, instead of just adding mortality numbers per stratum you may have a dataset with one row for each death and information on age and sex for each (or a significant proportion) of these deaths. In this case, you can aggregate this dataset by age and sex to create a dataset with numbers per stratum, and then add this to the dataset with population numbers per stratum. We will also show you how to do this.
Load packages
First, load the packages required for this analysis:
pacman::p_load(rio, # to import data
here, # to locate files
tidyverse, # to clean, handle, and plot the data (includes ggplot2 package)
dsr) # a package for directly standardized ratesThen, load the data
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
This page covers:
Load packages
One can use the R package DiagrammeR to create charts/flow charts. They can be static, or they can adjust somewhat dynamically based on changes in a dataset.
Tools
The function grViz() is used to create a “Graphviz” diagram. This function accepts a character string input containing instructions for making the diagram. Within that string, the instructions are written in a different language, called DOT - it is quite easy to learn the basics.
Basic structure
grViz("digraph my_flow_chart {}")Below are two simple examples
A very minimal example:
# A minimal plot
DiagrammeR::grViz("digraph {
graph[layout = dot, rankdir = LR]
a
b
c
a -> b -> c
}")An example with applied public health context:
grViz(" # All instructions are within a large character string
digraph surveillance_diagram { # 'digraph' means 'directional graph', then the graph name
# graph statement
#################
graph [layout = dot,
rankdir = TB,
overlap = true,
fontsize = 10]
# nodes
#######
node [shape = circle, # shape = circle
fixedsize = true
width = 1.3] # width of circles
Primary # names of nodes
Secondary
Tertiary
# edges
#######
Primary -> Secondary [label = 'case transfer']
Secondary -> Tertiary [label = 'case transfer']
}
")Basic syntax
Node names, or edge statements, can be separated with spaces, semicolons, or newlines.
Rank direction
A plot can be re-oriented to move left-to-right by adjusting the rankdir argument within the graph statement. The default is TB (top-to-bottom), but it can be LR (left-to-right), RL, or BT.
Node names
Node names can be single words, as in the simple example above. To use multi-word names or special characters (e.g. parentheses, dashes), put the node name within single quotes (’ ’). It may be easier to have a short node name, and assign a label, as shown below within brackets [ ]. A label is also necessary to have a newline within the node name - use \n in the node label within single quotes, as shown below.
Subgroups
Within edge statements, subgroups can be created on either side of the edge with curly brackets ({ }). The edge then applies to all nodes in the bracket - it is a shorthand.
Layouts
rankdir to either TB, LR, RL, BT, )Nodes - editable attributes
label (text, in single quotes if multi-word)fillcolor (many possible colors)fontcoloralpha (transparency 0-1)shape (ellipse, oval, diamond, egg, plaintext, point, square, triangle)stylesidesperipheriesfixedsize (h x w)heightwidthdistortionpenwidth (width of shape border)x (displacement left/right)y (displacement up/down)fontnamefontsizeiconEdges - editable attributes
arrowsizearrowhead (normal, box, crow, curve, diamond, dot, inv, none, tee, vee)arrowtaildir (direction, )style (dashed, …)coloralphaheadport (text in front of arrowhead)tailport (text in behind arrowtail)fontnamefontsizefontcolorpenwidth (width of arrow)minlen (minimum length)Color names: hexadecimal values or ‘X11’ color names, see here for X11 details
The example below expands on the surveillance_diagram, adding complex node names, grouped edges, colors and styling
grViz(" # All instructions are within a large character string
digraph surveillance_diagram { # 'digraph' means 'directional graph', then the graph name
# graph statement
#################
graph [layout = dot,
rankdir = TB, # layout top-to-bottom
fontsize = 10]
# nodes (circles)
#################
node [shape = circle, # shape = circle
fixedsize = true
width = 1.3]
Primary [label = 'Primary\nFacility']
Secondary [label = 'Secondary\nFacility']
Tertiary [label = 'Tertiary\nFacility']
SC [label = 'Surveillance\nCoordination',
fontcolor = darkgreen]
# edges
#######
Primary -> Secondary [label = 'case transfer',
fontcolor = red,
color = red]
Secondary -> Tertiary [label = 'case transfer',
fontcolor = red,
color = red]
# grouped edge
{Primary Secondary Tertiary} -> SC [label = 'case reporting',
fontcolor = darkgreen,
color = darkgreen,
style = dashed]
}
")Sub-graph clusters
To group nodes into boxed clusters, put them within the same named subgraph (subgraph name {}). To have the subgraph identified within a box, begin the name with “cluster” as shown below.
grViz(" # All instructions are within a large character string
digraph surveillance_diagram { # 'digraph' means 'directional graph', then the graph name
# graph statement
#################
graph [layout = dot,
rankdir = TB,
overlap = true,
fontsize = 10]
# nodes (circles)
#################
node [shape = circle, # shape = circle
fixedsize = true
width = 1.3] # width of circles
subgraph cluster_passive {
Primary [label = 'Primary\nFacility']
Secondary [label = 'Secondary\nFacility']
Tertiary [label = 'Tertiary\nFacility']
SC [label = 'Surveillance\nCoordination',
fontcolor = darkgreen]
}
# nodes (boxes)
###############
node [shape = box, # node shape
fontname = Helvetica] # text font in node
subgraph cluster_active {
Active [label = 'Active\nSurveillance'];
HCF_active [label = 'HCF\nActive Search']
}
subgraph cluster_EBD {
EBS [label = 'Event-Based\nSurveillance (EBS)'];
'Social Media'
Radio
}
subgraph cluster_CBS {
CBS [label = 'Community-Based\nSurveillance (CBS)'];
RECOs
}
# edges
#######
{Primary Secondary Tertiary} -> SC [label = 'case reporting']
Primary -> Secondary [label = 'case transfer',
fontcolor = red]
Secondary -> Tertiary [label = 'case transfer',
fontcolor = red]
HCF_active -> Active
{'Social Media'; Radio} -> EBS
RECOs -> CBS
}
")node shapes
The example below, borrowed from this tutorial, shows applied node shapes, and shows a shorthand for serial edge connections
DiagrammeR::grViz("digraph {
graph [layout = dot, rankdir = LR]
# define the global styles of the nodes. We can override these in box if we wish
node [shape = rectangle, style = filled, fillcolor = Linen]
data1 [label = 'Dataset 1', shape = folder, fillcolor = Beige]
data2 [label = 'Dataset 2', shape = folder, fillcolor = Beige]
process [label = 'Process \n Data']
statistical [label = 'Statistical \n Analysis']
results [label= 'Results']
# edge definitions with the node IDs
{data1 data2} -> process -> statistical -> results
}")How to handle and save outputs
“Parameterized figures: A great benefit of designing figures within R is that we are able to connect the figures directly with our analysis by reading R values directly into our flowcharts. For example, suppose you have created a filtering process which removes values after each stage of a process, you can have a figure show the number of values left in the dataset after each stage of your process. To do this we, you can use the @@X symbol directly within the figure, then refer to this in the footer of the plot using [X]:, where X is the a unique numeric index. Here is a basic example:”
https://mikeyharper.uk/flowcharts-in-r-using-diagrammer/
# Define some sample data
data <- list(a=1000, b=800, c=600, d=400)
DiagrammeR::grViz("
digraph graph2 {
graph [layout = dot]
# node definitions with substituted label text
node [shape = rectangle, width = 4, fillcolor = Biege]
a [label = '@@1']
b [label = '@@2']
c [label = '@@3']
d [label = '@@4']
a -> b -> c -> d
}
[1]: paste0('Raw Data (n = ', data$a, ')')
[2]: paste0('Remove Errors (n = ', data$b, ')')
[3]: paste0('Identify Potential Customers (n = ', data$c, ')')
[4]: paste0('Select Top Priorities (n = ', data$d, ')')
")Much of the above is adapted from the tutorial at this site
Other more in-depth tutorial: http://rich-iannone.github.io/DiagrammeR/
Note above is out of date via DiagrammeR
Plotting the connections in a dataset
https://www.r-graph-gallery.com/321-introduction-to-interactive-sankey-diagram-2.html
Counts of age category and hospital, relabled as target and source, respectively.
# counts by hospital and age category
links <- linelist %>%
select(hospital, age_cat) %>%
count(hospital, age_cat) %>%
rename(source = hospital,
target = age_cat)Now formalize the nodes list, and adjust the ID columns to be numbers instead of labels:
# The unique node names
nodes <- data.frame(
name=c(as.character(links$source), as.character(links$target)) %>%
unique()
)
# match to numbers, not names
links$IDsource <- match(links$source, nodes$name)-1
links$IDtarget <- match(links$target, nodes$name)-1Now plot the Sankey diagram:
# plot
######
p <- sankeyNetwork(Links = links,
Nodes = nodes,
Source = "IDsource",
Target = "IDtarget",
Value = "n",
NodeID = "name",
units = "TWh",
fontSize = 12,
nodeWidth = 30)
pHere is an example where the patient Outome is included as well. Note in the data management step how we bind rows of counts of hospital -> outcome, using the same column names.
# counts by hospital and age category
links <- linelist %>%
select(hospital, age_cat) %>%
mutate(age_cat = stringr::str_glue("Age {age_cat}")) %>%
count(hospital, age_cat) %>%
rename(source = age_cat,
target = hospital) %>%
bind_rows(
linelist %>%
select(hospital, outcome) %>%
count(hospital, outcome) %>%
rename(source = hospital,
target = outcome)
)
# The unique node names
nodes <- data.frame(
name=c(as.character(links$source), as.character(links$target)) %>%
unique()
)
# match to numbers, not names
links$IDsource <- match(links$source, nodes$name)-1
links$IDtarget <- match(links$target, nodes$name)-1
# plot
######
p <- sankeyNetwork(Links = links,
Nodes = nodes,
Source = "IDsource",
Target = "IDtarget",
Value = "n",
NodeID = "name",
units = "TWh",
fontSize = 12,
nodeWidth = 30)
phttps://www.displayr.com/sankey-diagrams-r/
Timeline Sankey - LTFU from cohort… application/rejections… etc.
E.g. border closures during COVID TO DO need to change to non-sensitive example
# IHR timeline of travel restrictions by time and by country
# reference: https://cran.r-project.org/web/packages/vistime/vignettes/vistime-vignette.html#ex.-2-project-planning
library(vistime)
data <- read.csv(text="event,group,start,end,color
Entry ban, DPRK, 2020-01-22, 2020-01-22, #90caf9
Entry ban, Samoa, 2020-01-23, 2020-01-23, #90caf9
Flights suspension, Republic of Korea,2020-01-23,2020-01-23, #1565c0
Exit ban, People's Republic of China, 2020-01-25,2020-01-25, #f44336
Entry ban, Marshall Islands, 2020-01-25, 2020-01-25, #90caf9
Visa restriction, Kazakhstan, 2020-01-26, 2020-01-26, #8d6e63
Flights suspension, Egypt,2020-01-27,2020-01-27, #1565c0
Entry ban, Malaysia, 2020-01-27, 2020-01-27, #90caf9
Border closure, Mongolia, 2020-01-27,2020-01-27,#90a4ae
Visa restriction, Kazakhstan, 2020-01-28,2020-01-28,#fc8d62
Isolation without symptoms, Kyrgyzstan, 2020-01-28,2020-01-28, #6a3d9a
Entry ban, Philippines, 2020-01-28, 2020-01-28, #90caf9
Visa refusal, Philippines, 2020-01-28, 2020-01-28, #fc8d62
Flights suspension, Philippines, 2020-01-28, 2020-01-28, #1565c0
Entry ban, Palau, 2020-01-29, 2020-01-29, #90caf9
Entry ban, Russian Federation, 2020-01-29, 2020-01-29, #90caf9
Border closure, Russian Federation, 2020-01-29,2020-01-29,#90a4ae
Flights suspension, United Kingdom (BA),2020-01-29,2020-01-29, #1565c0
Flight reduced, United States of America (United),2020-01-29,2020-01-29, #1565c0
Flights suspension, Germany (Lufthansa),2020-01-29,2020-01-29, #1565c0
Entry ban, Papua New Guinea, 2020-01-29, 2020-01-29, #90caf9
")
p <- vistime(data)
library(plotly)
# step 1: transform into a list
pp <- plotly_build(p)
# step 2: Marker size
for(i in 1:length(pp$x$data)){
if(pp$x$data[[i]]$mode == "markers") pp$x$data[[i]]$marker$size <- 10
}
# step 2: text size
for(i in 1:length(pp$x$data)){
if(pp$x$data[[i]]$mode == "text") pp$x$data[[i]]$textfont$size <- 10
}
# step : text position
for(i in 1:length(pp$x$data)){
if(pp$x$data[[i]]$mode == "text") pp$x$data[[i]]$textposition <- "right"
}
ppThis tab should stay with the name “Resources”. Links to other online tutorials or resources.
Age pyramids can be useful to show patterns by age group. They can show gender, or the distribution of other characteristics.
These tabs demonstrate how to produce age pyramids using:
ggplot()Age/gender demographic pyramids in R are generally made with ggplot() by creating two barplots (one for each gender), converting one’s values to negative values, and flipping the x and y axes to display the barplots vertically.
Here we offer a quick approach through the apyramid package:
ggplot() commandsFor this tab we use the linelist dataset that is cleaned in the Cleaning tab.
To make a traditional age/sex demographic pyramid, the data must first be cleaned in the following ways:
Load packages
First, load the packages required for this analysis:
pacman::p_load(rio, # to import data
here, # to locate files
tidyverse, # to clean, handle, and plot the data (includes ggplot2 package)
apyramid, # a package dedicated to creating age pyramids
stringr) # working with strings for titles, captions, etc.Load the data
Check class of variables
Ensure that the age variable is class Numeric, and check the class and order of levels of age_cat and age_cat5
class(linelist$age_years)
## [1] "numeric"
class(linelist$age_cat)
## [1] "factor"
class(linelist$age_cat5)
## [1] "factor"
table(linelist$age_cat, useNA = "always")
##
## 0-4 5-9 10-14 15-19 20-29 30-49 50-69 70+ <NA>
## 1054 1153 957 889 1065 658 24 0 88
table(linelist$age_cat5, useNA = "always")
##
## 0-4 5-9 10-14 15-19 20-24 25-29 30-34 35-39 40-44 45-49 50-54 55-59 60-64
## 1054 1153 957 889 570 495 315 171 113 59 14 6 2
## 65-69 70-74 75-79 80-84 85+ <NA>
## 2 0 0 0 0 88The package apyramid allows you to quickly make an age pyramid. For more nuanced situations, see the tab on using ggplot() to make age pyramids. You can read more about the apyramid package in its Help page by entering ?age_pyramid in your R console.
Using the cleaned linelist dataset, we can create an age pyramid with just one simple command. If you need help cleaning your data, see the handbook page on Cleaning data (LINK). In this command:
linelist dataframeage_cat5) When using agepyramid package, if the
split_by column is binary (e.g. male/female, or yes/no), then the result will appear as a pyramid. However if there are more than two values in the split_by column (not including NA), the pyramid will appears as a faceted barplot with empty bars in the background indicating the range of the un-faceted data set for the age group. Values of split_by will appear as labels at top of each facet. For example below if the split_by variable is “hospital”.
apyramid::age_pyramid(data = linelist,
age_group = "age_cat5",
split_by = "hospital",
na.rm = FALSE) # show a bar for patients missing age, (note: this changes the pyramid into a faceted barplot)Missing values
Rows missing values for the split_by or age_group columns, if coded as NA, will not trigger the faceting shown above. By default these rows will not be shown. However you can specify that they appear, in an adjacent barplot and as a separate age group at the top, by specifying na.rm = FALSE.
apyramid::age_pyramid(data = linelist,
age_group = "age_cat5",
split_by = "gender",
na.rm = FALSE) # show patients missing age or genderProportions, colors, & aesthetics
By default, the bars display counts (not %), a dashed mid-line for each group is shown, and the colors are green/purple. Each of these parameters can all be adjusted, as shown below:
You can also add additional ggplot() commands to the plot using the standard ggplot() “+” syntax, such as aesthetic themes and label adjustments:
apyramid::age_pyramid(data = linelist,
age_group = "age_cat5",
split_by = "gender",
proportional = TRUE, # show percents, not counts
show_midpoint = FALSE, # remove bar mid-point line
#pal = c("orange", "purple") # can specify alt. colors here (but not labels, see below)
)+
# additional ggplot commands
theme_minimal()+ # simplify the background
scale_fill_manual(values = c("orange", "purple"), # to specify colors AND labels
labels = c("Male", "Female"))+
labs(y = "Percent of all cases", # note that x and y labels are switched (see ggplot tab)
x = "Age categories",
fill = "Gender",
caption = "My data source and caption here",
title = "Title of my plot",
subtitle = "Subtitle with \n a second line...")+
theme(
legend.position = "bottom", # move legend to bottom
axis.text = element_text(size = 10, face = "bold"), # fonts/sizes, see ggplot tips page
axis.title = element_text(size = 12, face = "bold"))The examples above assume your data are in a linelist-like format, with one row per observation. If your data are already aggregated into counts by age category, you can still use the apyramid package, as shown below.
Let’s say that your dataset looks like this, with columns for age category, and male counts, female counts, and missing counts.
(see the handbook page on Transforming data for tips)
# View the aggregated data
DT::datatable(demo_agg, rownames = FALSE, filter="top", options = list(pageLength = 5, scrollX=T) )ggplot() perfers data in “long” format, so first pivot the data to be “long” with the pivot_longer() function from dplyr.
# pivot the aggregated data into long format
demo_agg_long <- demo_agg %>%
pivot_longer(c(f, m, missing_gender), # cols to elongate
names_to = "gender", # name for new col of categories
values_to = "counts") %>% # name for new col of counts
mutate(gender = na_if(gender, "missing_gender")) # convert "missing_gender" to NA# View the aggregated data
DT::datatable(demo_agg_long, rownames = FALSE, filter="top", options = list(pageLength = 5, scrollX=T) )Then use the split_by and count arguments of age_pyramid() to specify the respective columns:
apyramid::age_pyramid(data = demo_agg_long,
age_group = "age_cat5",
split_by = "gender",
count = "counts") # give the column name for the aggregated countsNote in the above, that the factor order of “m” and “f” is different (pyramid reversed). To adjust the order you must re-define gender in the aggredated data as a Factor and order the levels as desired.
ggplot()ggplot()
Using ggplot() to build your age pyramid allows for more flexibility, but requires more effort and understanding of how ggplot() works. It is also easier to accidentally make mistakes.
apyramid uses ggplot() in the background (and accepts ggplot() commands added), but this page shows how to adjust or recreate a pyramid only using ggplot(), if you wish.
First, understand that to make such a pyramid using ggplot() the approach is to:
Within the ggplot(), create two graphs by age category. Create one for each of the two grouping values (in this case gender). See filters applied to the data arguments in each geom_histogram() commands below.
If using geom_histogram(), the graphs operate off the numeric column (e.g. age_years), whereas if using geom_barplot() the graphs operate from an ordered Factor (e.g. age_cat5).
One graph will have positive count values, while the other will have its counts converted to negative values - this allows both graphs to be seen and compared against each other in the same plot.
The command coord_flip() switches the X and Y axes, resulting in the graphs turning vertical and creating the pyramid.
Lastly, the counts-axis labels must be specified so they appear as “positive” counts on both sides of the pyramid (despite the underlying values on one side being negative).
A simple version of this, using geom_histogram(), is below:
# begin ggplot
ggplot(data = linelist, aes(x = age, fill = gender)) +
# female histogram
geom_histogram(data = filter(linelist, gender == "f"),
breaks = seq(0,85,5),
colour = "white") +
# male histogram (values converted to negative)
geom_histogram(data = filter(linelist, gender == "m"),
breaks = seq(0,85,5),
aes(y=..count..*(-1)),
colour = "white") +
# flip the X and Y axes
coord_flip() +
# adjust counts-axis scale
scale_y_continuous(limits = c(-600, 900),
breaks = seq(-600,900,100),
labels = abs(seq(-600, 900, 100)))DANGER: If the limits of your counts axis are set too low, and a counts bar exceeds them, the bar will disappear entirely or be artificially shortened! Watch for this if analyzing data which is routinely updated. Prevent it by having your count-axis limits auto-adjust to your data, as below.
There are many things you can change/add to this simple version, including:
# create dataset with proportion of total
pyramid_data <- linelist %>%
group_by(age_cat5, gender) %>%
summarize(counts = n()) %>%
ungroup() %>%
mutate(percent = round(100*(counts / sum(counts, na.rm=T)),1),
percent = case_when(
gender == "f" ~ percent,
gender == "m" ~ -percent,
TRUE ~ NA_real_))
max_per <- max(pyramid_data$percent, na.rm=T)
min_per <- min(pyramid_data$percent, na.rm=T)
# begin ggplot
ggplot()+ # default x-axis is age in years;
# case data graph
geom_bar(data = pyramid_data,
stat = "identity",
aes(x = age_cat5,
y = percent,
fill = gender), #
colour = "white")+ # white around each bar
# flip the X and Y axes to make pyramid vertical
coord_flip()+
# adjust the axes scales (remember they are flipped now!)
#scale_x_continuous(breaks = seq(0,100,5), labels = seq(0,100,5)) +
scale_y_continuous(limits = c(min_per, max_per),
breaks = seq(floor(min_per), ceiling(max_per), 2),
labels = paste0(abs(seq(floor(min_per), ceiling(max_per), 2)), "%"))+
# designate colors and legend labels manually
scale_fill_manual(
values = c("f" = "orange",
"m" = "darkgreen"),
labels = c("Female", "Male"),
) +
# label values (remember X and Y flipped now)
labs(
x = "Age group",
y = "Percent of total",
fill = NULL,
caption = stringr::str_glue("Data are from linelist \nn = {nrow(linelist)} (age or sex missing for {sum(is.na(linelist$gender) | is.na(linelist$age_years))} cases) \nData as of: {format(Sys.Date(), '%d %b %Y')}")) +
# optional aesthetic themes
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
plot.title = element_text(hjust = 0.5),
plot.caption = element_text(hjust=0, size=11, face = "italic")) +
ggtitle(paste0("Age and gender of cases"))With the flexibility of ggplot(), you can have a second layer of bars in the background that represent the true population pyramid. This can provide a nice visualization to compare the observed counts with the baseline.
Import and view the population data
# display the linelist data as a table
DT::datatable(pop, rownames = FALSE, filter="top", options = list(pageLength = 10, scrollX=T) )First some data management steps:
Here we record the order of age categories that we want to appear. Due to some quirks the way the ggplot() is implemented, it is easiest to store these as a character vector and use them later in the plotting function.
# record correct age cat levels
age_levels <- c("0-4","5-9", "10-14", "15-19", "20-24",
"25-29","30-34", "35-39", "40-44", "45-49",
"50-54", "55-59", "60-64", "65-69", "70-74",
"75-79", "80-84", "85+")Combine the population and case data through the dplyr function bind_rows():
bind_rows())# create/transform populaton data, with percent of total
########################################################
pop_data <- pivot_longer(pop, c(m, f), names_to = "gender", values_to = "counts") %>% # pivot gender columns longer
mutate(data = "population", # add column designating data source
percent = round(100*(counts / sum(counts, na.rm=T)),1), # calculate % of total
percent = case_when( # if male, convert % to negative
gender == "f" ~ percent,
gender == "m" ~ -percent,
TRUE ~ NA_real_))Review the changed population dataset
# display the linelist data as a table
DT::datatable(pop_data, rownames = FALSE, filter="top", options = list(pageLength = 5, scrollX=T) )Now implement the same for the case linelist. Slightly different because it begins with case-rows, not counts.
# create case data by age/gender, with percent of total
#######################################################
case_data <- linelist %>%
group_by(age_cat5, gender) %>% # aggregate linelist cases into age-gender groups
summarize(counts = n()) %>% # calculate counts per age-gender group
ungroup() %>%
mutate(data = "cases", # add column designating data source
percent = round(100*(counts / sum(counts, na.rm=T)),1), # calculate % of total for age-gender groups
percent = case_when( # convert % to negative if male
gender == "f" ~ percent,
gender == "m" ~ -percent,
TRUE ~ NA_real_))Review the changed case dataset
# display the linelist data as a table
DT::datatable(case_data, rownames = FALSE, filter="top", options = list(pageLength = 5, scrollX=T) )Now the two datasets are combined, one on top of the other (same column names)
# combine case and population data (same column names, age_cat values, and gender values)
pyramid_data <- bind_rows(case_data, pop_data)Store the maximum and minimum percent values, used in the plotting funtion to define the extent of the plot (and not cut off any bars!)
# Define extent of percent axis, used for plot limits
max_per <- max(pyramid_data$percent, na.rm=T)
min_per <- min(pyramid_data$percent, na.rm=T)Now the plot is made with ggplot():
# begin ggplot
##############
ggplot()+ # default x-axis is age in years;
# population data graph
geom_bar(data = filter(pyramid_data, data == "population"),
stat = "identity",
aes(x = age_cat5,
y = percent,
fill = gender),
colour = "black", # black color around bars
alpha = 0.2, # more transparent
width = 1)+ # full width
# case data graph
geom_bar(data = filter(pyramid_data, data == "cases"),
stat = "identity", # use % as given in data, not counting rows
aes(x = age_cat5, # age categories as original X axis
y = percent, # % as original Y-axis
fill = gender), # fill of bars by gender
colour = "black", # black color around bars
alpha = 1, # not transparent
width = 0.3)+ # half width
# flip the X and Y axes to make pyramid vertical
coord_flip()+
# adjust axes order, scale, and labels (remember X and Y axes are flipped now)
# manually ensure that age-axis is ordered correctly
scale_x_discrete(limits = age_levels)+
# set percent-axis
scale_y_continuous(limits = c(min_per, max_per), # min and max defined above
breaks = seq(floor(min_per), ceiling(max_per), by = 2), # from min% to max% by 2
labels = paste0( # for the labels, paste together...
abs(seq(floor(min_per), ceiling(max_per), by = 2)), # ...rounded absolute values of breaks...
"%"))+ # ... with "%"
# floor(), ceiling() round down and up
# designate colors and legend labels manually
scale_fill_manual(
values = c("f" = "orange", # assign colors to values in the data
"m" = "darkgreen"),
labels = c("f" = "Female",
"m"= "Male"), # change labels that appear in legend, note order
) +
# plot labels, titles, caption
labs(
title = "Case age and gender distribution,\nas compared to baseline population",
subtitle = "",
x = "Age category",
y = "Percent of total",
fill = NULL,
caption = stringr::str_glue("Cases shown on top of country demographic baseline\nCase data are from linelist, n = {nrow(linelist)}\nAge or gender missing for {sum(is.na(linelist$gender) | is.na(linelist$age_years))} cases\nCase data as of: {format(max(linelist$date_onset, na.rm=T), '%d %b %Y')}")) +
# optional aesthetic themes
theme(
legend.position = "bottom", # move legend to bottom
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
plot.title = element_text(hjust = 0),
plot.caption = element_text(hjust=0, size=11, face = "italic"))The techniques used to make a population pyramid with ggplot() can also be used to make plots of Likert-scale survey data.
Import the data
Start with data that looks like this, with a categorical classification of each respondent (status) and their answers to 8 questions on a 4-point Likert-type scale (“Very poor”, “Poor”, “Good”, “Very good”).
# display the linelist data as a table
DT::datatable(likert_data, rownames = FALSE, filter="top", options = list(pageLength = 10, scrollX=T) )First, some data management steps:
direction depending on whether response was generally “positive” or “negative”status column and the Response columnmelted <- pivot_longer(likert_data, Q1:Q8, names_to = "Question", values_to = "Response") %>%
mutate(direction = case_when(
Response %in% c("Poor","Very Poor") ~ "Negative",
Response %in% c("Good", "Very Good") ~ "Positive",
TRUE ~ "Unknown"),
status = factor(status, levels = rev(c(
"Senior", "Intermediate", "Junior"))),
Response = factor(Response, levels = c("Very Good", "Good",
"Very Poor", "Poor"))) # must reverse Very Poor and Poor for ordering to work
melted_max <- melted %>%
group_by(status, Question) %>%
summarize(n = n())
melted_max <- max(melted_max$n, na.rm=T)Now make the plot:
# make plot
ggplot()+
# bar graph of the "negative" responses
geom_bar(data = filter(melted,
direction == "Negative"),
aes(x = status,
y=..count..*(-1), # counts inverted to negative
fill = Response),
color = "black",
closed = "left",
position = "stack")+
# bar graph of the "positive responses
geom_bar(data = filter(melted, direction == "Positive"),
aes(x = status, fill = Response),
colour = "black",
closed = "left",
position = "stack")+
# flip the X and Y axes
coord_flip()+
# Black vertical line at 0
geom_hline(yintercept = 0, color = "black", size=1)+
# convert labels to all positive numbers
scale_y_continuous(limits = c(-ceiling(melted_max/10)*11, ceiling(melted_max/10)*10), # seq from neg to pos by 10, edges rounded outward to nearest 5
breaks = seq(-ceiling(melted_max/10)*10, ceiling(melted_max/10)*10, 10),
labels = abs(unique(c(seq(-ceiling(melted_max/10)*10, 0, 10),
seq(0, ceiling(melted_max/10)*10, 10))))) +
# color scales manually assigned
scale_fill_manual(values = c("Very Good" = "green4", # assigns colors
"Good" = "green3",
"Poor" = "yellow",
"Very Poor" = "red3"),
breaks = c("Very Good", "Good", "Poor", "Very Poor"))+ # orders the legend
# facet the entire plot so each question is a sub-plot
facet_wrap(~Question, ncol = 3)+
# labels, titles, caption
labs(x = "Respondent status",
y = "Number of responses",
fill = "")+
ggtitle(str_glue("Likert-style responses\nn = {nrow(likert_data)}"))+
# aesthetic settings
theme_minimal()+
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 14, face = "bold"),
strip.text = element_text(size = 14, face = "bold"), # facet sub-titles
plot.title = element_text(size = 20, face = "bold"),
panel.background = element_rect(fill = NA, color = "black")) # black box around each facetThis tab should stay with the name “Resources”. Links to other online tutorials or resources.
Heatmaps can be useful when tracking reporting metrics across many facilities/jurisdictions over time
For example, the image below shows % of weekdays that data was received from each facility, week-by-week:
Often in public health, an objective is to assess trends over time for many entities (facilities, jurisdictions, etc.). One way to visualize trends over time from many entities is a heatmap where the x-axis is time and the y-axis are the many entities.
To demonstrate this, we import this dataset of daily malaria case reports from 65 facilities.
The preparation will involve:
Below are the first 30 rows of these data:
The packages we will use are:
The objective is to transform the daily reports (seen in previous tab) into weekly reports with a summary of performance - in this case the proportion of days per week that the facility reported any data for Spring District from April-May 2019.
To achieve this:
date2week() from package aweek
floor_day = argument means that dates are rounded into the week only (day of the week is not shown)factor = argument converts the new column to a factor - important because all possible weeks within the date range are designated as levels, even if there is no data for them currently.summarize() creates new columns to calculate reporting performance for each “facility-week”:
right_join()) to a comprehensive list of all possible facility-week combinations, to make the dataset complete. The matrix of all possible combinations is created by applying expand() to those two columns of the dataframe as it is at that moment in the pipe chain (represented by “.”). Because a right_join() is used, all rows in the expand() dataframe are kept, and added to agg_weeks if necessary. These new rows appear with NA (missing) summarized values.# Create weekly summary dataset
agg_weeks <- facility_count_data %>%
# filter the data as appropriate
filter(District == "Spring",
data_date < as.Date("2019-06-01")) %>%
# Create week column from data_date
mutate(week = aweek::date2week(data_date,
start_date = "Monday",
floor_day = TRUE,
factor = TRUE)) %>%
# Group into facility-weeks
group_by(location_name, week, .drop = F) %>%
# Create summary column on the grouped data
summarize(n_days = 7, # 7 days per week
n_reports = dplyr::n(), # number of reports received per week (could be >7)
malaria_tot = sum(malaria_tot, na.rm = T), # total malaria cases reported
n_days_reported = length(unique(data_date)), # number of unique days reporting per week
p_days_reported = round(100*(n_days_reported / n_days))) %>% # percent of days reporting
# Ensure every possible facility-week combination appears in the data
right_join(expand(., week, location_name)) # "." represents the dataset at that moment in the pipe chain
The ggplot() is make using geom_tile():
scale_x_date()fill is the performance for that facility-week (numeric)scale_fill_gradient() is used on the numeric fill, specifying colors for high, low, and NAscale_x_date() is used on the x-axis specifying labels every 2 weeks and their formatggplot(agg_weeks,
aes(x = aweek::week2date(week), # transformed to date class
y = location_name,
fill = p_days_reported))+
# tiles
geom_tile(colour="white")+ # white gridlines
scale_fill_gradient(low = "orange", high = "darkgreen", na.value = "grey80")+
scale_x_date(expand = c(0,0),
date_breaks = "2 weeks",
date_labels = "%d\n%b")+
# aesthetic themes
theme_minimal()+ # simplify background
theme(
legend.title = element_text(size=12, face="bold"),
legend.text = element_text(size=10, face="bold"),
legend.key.height = grid::unit(1,"cm"), # height of legend key
legend.key.width = grid::unit(0.6,"cm"), # width of legend key
axis.text.x = element_text(size=12),
axis.text.y = element_text(vjust=0.2),
axis.ticks = element_line(size=0.4),
axis.title = element_text(size=12, face="bold"),
plot.title = element_text(hjust=0,size=14,face="bold"),
plot.caption = element_text(hjust = 0, face = "italic")
)+
# plot labels
labs(x = "Week",
y = "Facility name",
fill = "Reporting\nperformance (%)", # legend title
title = "Percent of days per week that facility reported data",
subtitle = "District health facilities, April-May 2019",
caption = "7-day weeks beginning on Mondays.")If you want to order the y-axis facilities by something, convert them to class Factor and provide the order. Below, the order is set based on the total number of reporting days filed by the facility across the whole timespan:
facility_order <- agg_weeks %>%
group_by(location_name) %>%
summarize(tot_reports = sum(n_days_reported, na.rm=T)) %>%
arrange(tot_reports) # ascending orderas.tibble(facility_order)
## # A tibble: 15 x 2
## location_name tot_reports
## <chr> <int>
## 1 Facility 56 1
## 2 Facility 65 6
## 3 Facility 11 19
## 4 Facility 39 31
## 5 Facility 59 33
## 6 Facility 27 40
## 7 Facility 32 41
## 8 Facility 51 41
## 9 Facility 7 42
## 10 Facility 1 46
## 11 Facility 9 48
## 12 Facility 35 50
## 13 Facility 50 51
## 14 Facility 58 53
## 15 Facility 28 75Now use the above vector (facility_order$location_name) to be the order of the factor levels of location_name in the dataset agg_weeks:
agg_weeks <- agg_weeks %>%
mutate(location_name = factor(location_name, levels = facility_order$location_name))And now the data are re-plotted, with location_name being an ordered factor:
ggplot(agg_weeks,
aes(x = aweek::week2date(week), # transformed to date class
y = location_name,
fill = p_days_reported))+
# tiles
geom_tile(colour="white")+ # white gridlines
scale_fill_gradient(low = "orange", high = "darkgreen", na.value = "grey80")+
scale_x_date(expand = c(0,0),
date_breaks = "2 weeks",
date_labels = "%d\n%b")+
# aesthetic themes
theme_minimal()+ # simplify background
theme(
legend.title = element_text(size=12, face="bold"),
legend.text = element_text(size=10, face="bold"),
legend.key.height = grid::unit(1,"cm"), # height of legend key
legend.key.width = grid::unit(0.6,"cm"), # width of legend key
axis.text.x = element_text(size=12),
axis.text.y = element_text(vjust=0.2),
axis.ticks = element_line(size=0.4),
axis.title = element_text(size=12, face="bold"),
plot.title = element_text(hjust=0,size=14,face="bold"),
plot.caption = element_text(hjust = 0, face = "italic")
)+
# plot labels
labs(x = "Week",
y = "Facility name",
fill = "Reporting\nperformance (%)", # legend title
title = "Percent of days per week that facility reported data",
subtitle = "District health facilities, April-May 2019",
caption = "7-day weeks beginning on Mondays.")You can add a geom_text() layer on top of the tiles, to display the actual numbers of each tile. Be aware this may not look pretty if you have many small tiles!
geom_text(aes(label=p_days_reported))+. In the aesthetic aes() of the geom_tile() the argument label (what to show) is set to the same numeric column used to create the color gradient.ggplot(agg_weeks,
aes(x = aweek::week2date(week), # transformed to date class
y = location_name,
fill = p_days_reported))+
# tiles
geom_tile(colour="white")+ # white gridlines
geom_text(aes(label = p_days_reported))+ # add text on top of tile
scale_fill_gradient(low = "orange", high = "darkgreen", na.value = "grey80")+
scale_x_date(expand = c(0,0),
date_breaks = "2 weeks",
date_labels = "%d\n%b")+
# aesthetic themes
theme_minimal()+ # simplify background
theme(
legend.title = element_text(size=12, face="bold"),
legend.text = element_text(size=10, face="bold"),
legend.key.height = grid::unit(1,"cm"), # height of legend key
legend.key.width = grid::unit(0.6,"cm"), # width of legend key
axis.text.x = element_text(size=12),
axis.text.y = element_text(vjust=0.2),
axis.ticks = element_line(size=0.4),
axis.title = element_text(size=12, face="bold"),
plot.title = element_text(hjust=0,size=14,face="bold"),
plot.caption = element_text(hjust = 0, face = "italic")
)+
# plot labels
labs(x = "Week",
y = "Facility name",
fill = "Reporting\nperformance (%)", # legend title
title = "Percent of days per week that facility reported data",
subtitle = "District health facilities, April-May 2019",
caption = "7-day weeks beginning on Mondays.")Contoured heatmap of cases over a basemap
linelist using the latitude and longitudehttp://data-analytics.net/cep/Schedule_files/geospatial.html
pacman::p_load(OpenStreetMap)
# Fit basemap by range of lat/long coordinates. Choose tile type
map <- openmap(c(max(linelist$lat, na.rm=T), max(linelist$lon, na.rm=T)), # limits of tile
c(min(linelist$lat, na.rm=T), min(linelist$lon, na.rm=T)),
zoom = NULL,
type = c("osm", "stamen-toner", "stamen-terrain","stamen-watercolor", "esri","esri-topo")[1],
mergeTiles = TRUE)
# Projection WGS84
map.latlon <- openproj(map, projection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
# Plot map. Must be autoplotted to work with ggplot
OpenStreetMap::autoplot.OpenStreetMap(map.latlon)+
# Density tiles
ggplot2::stat_density_2d(aes(x = lon,
y = lat,
fill = ..level..,
alpha=..level..),
bins = 10,
geom = "polygon",
contour_var = "count",
data = linelist,
show.legend = F) +
scale_fill_gradient(low = "black", high = "red")+
labs(x = "Longitude",
y = "Latitude",
title = "Distribution of simulated cases") https://www.rdocumentation.org/packages/OpenStreetMap/versions/0.3.4/topics/autoplot.OpenStreetMap
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
This analysis plots the frequency of different combinations of values/responses. In this example, we plot the frequency of symptom combinations.
This analysis is often called:
Multiple response analysis Sets analysis Combinations analysis
The first method shown uses the package ggupset, an the second using the package UpSetR.
An example plot is below. Five symptoms are shown. Below each vertical bar is a line and dots indicating the combination of symptoms reflected by the bar above. To the right, horizontal bars reflect the frequency of each individual symptom.
This linelist includes five “yes/no” variables on reported symptoms. We will need to transform these variables a bit to use the ggupset package to make our plot.
View the data (scroll to the right to see the symptoms variables)
We convert the “yes” and “no the the actual symptom name. If”no", we set the value as blank.
# create column with the symptoms named, separated by semicolons
linelist_sym_1 <- linelist_sym %>%
# convert the "yes" and "no" values into the symptom name itself
mutate(fever = case_when(fever == "yes" ~ "fever", # if old value is "yes", new value is "fever"
TRUE ~ NA_character_), # if old value is anything other than "yes", the new value is NA
chills = case_when(chills == "yes" ~ "chills",
TRUE ~ NA_character_),
cough = case_when(cough == "yes" ~ "cough",
TRUE ~ NA_character_),
aches = case_when(aches == "yes" ~ "aches",
TRUE ~ NA_character_),
shortness_of_breath = case_when(shortness_of_breath == "yes" ~ "shortness_of_breath",
TRUE ~ NA_character_))Now we make two final variables:
1. Pasting together all the symptoms of the patient (character variable)
2. Convert the above to class list, so it can be accepted by ggupset to make the plot
linelist_sym_1 <- linelist_sym_1 %>%
mutate(
# combine the variables into one, using paste() with a semicolon separating any values
all_symptoms = paste(fever, chills, cough, aches, shortness_of_breath, sep = "; "),
# make a copy of all_symptoms variable, but of class "list" (which is required to use ggupset() in next step)
all_symptoms_list = as.list(strsplit(all_symptoms, "; "))
)View the new data. Note the two columns at the end - the pasted combined values, and the list
ggupsetLoad required package to make the plot (ggupset)
Create the plot:
ggplot(linelist_sym_1,
aes(x=all_symptoms_list)) +
geom_bar() +
scale_x_upset(reverse = FALSE,
n_intersections = 10,
sets = c("fever", "chills", "cough", "aches", "shortness_of_breath")
)+
labs(title = "Signs & symptoms",
subtitle = "10 most frequent combinations of signs and symptoms",
caption = "Caption here.",
x = "Symptom combination",
y = "Frequency in dataset")More information on ggupset can be found online or offline in the package documentation in your RStudio Help tab.
UpSetRThe UpSetR package allows more customization, but it more difficult to execute:
https://github.com/hms-dbmi/UpSetR read this https://gehlenborglab.shinyapps.io/upsetr/ Shiny App version - you can upload your own data https://cran.r-project.org/web/packages/UpSetR/UpSetR.pdf documentation - difficult to interpret
Convert symptoms variables to 1/0.
# Make using upSetR
linelist_sym_2 <- linelist_sym %>%
# convert the "yes" and "no" values into the symptom name itself
mutate(fever = case_when(fever == "yes" ~ 1, # if old value is "yes", new value is "fever"
TRUE ~ 0), # if old value is anything other than "yes", the new value is NA
chills = case_when(chills == "yes" ~ 1,
TRUE ~ 0),
cough = case_when(cough == "yes" ~ 1,
TRUE ~ 0),
aches = case_when(aches == "yes" ~ 1,
TRUE ~ 0),
shortness_of_breath = case_when(shortness_of_breath == "yes" ~ 1,
TRUE ~ 0))Now make the plot, using only the symptom variables. Must designate which “sets” to compare (the names of the symptom variables).
Alternatively use nsets = and order.by = "freq" to only show the top X combinations.
# Make the plot
UpSetR::upset(
select(linelist_sym_2, fever, chills, cough, aches, shortness_of_breath),
sets = c("fever", "chills", "cough", "aches", "shortness_of_breath"),
order.by = "freq",
sets.bar.color = c("blue", "red", "yellow", "darkgreen", "orange"), # optional colors
empty.intersections = "on",
# nsets = 3,
number.angles = 0,
point.size = 3.5,
line.size = 2,
mainbar.y.label = "Symptoms Combinations",
sets.x.label = "Patients with Symptom")This tab should stay with the name “Resources”. Links to other online tutorials or resources.
R Markdown is a fantastic tool for creating automated, reproducible, and share-worthy outputs. It can generate static or interactive outputs, in the form of html, word, pdf, powerpoint, and others.
Using markdown will allow you easily recreate an entire formatted document, including tables/figures/text, using new data (e.g. daily surveillance reports) and/or subsets of data (e.g. reports for specific geographies).
This guide will go through the basics. See ‘resources’ tab for further info.
Preparation of an R Markdown workflow involves ensuring you have set up an R project and have a folder structure that suits the desired workflow.
For instance, you may want an ‘output’ folder for your rendered documents, an ‘input’ folder for new cleaned data files, as well as subfolders within them which are date-stamped or reflect the subgeographies of interest. The markdown itself can go in a ‘rmd’ subfolder, particularly if you have multiple Rmd files within the same project.
You can set code up to create output subfolders for you each time you run reports (see “Producing an output”), but you should have the overall design in mind.
Because R Markdown can run into pandoc issues when running on a shared network drive, it is recommended that your folder is on your local machine, e.g. in a project within ‘My Documents’. If you use Git (much recommended!), this will be familiar.
An R Markdown document looks like and can be edited just like a standard R script, in R Studio. However, it contains more than just the usual R code and hashed comments. There are three basic components:
1. Metadata: This is referred to as the ‘YAML metadata’ and is at the top of the R Markdown document between two ‘—‘s. It will tell your Rmd file what type of output to produce, formatting preferences, and other metadata sucsh as document title, author, and date. There are other uses not mentioned here (but referred to in ‘Producing an output’). Note that indentation matters.
2. Text: This is the narrative of your document, including the titles. It is written in the markdown language, used across many different programmes. This means you can add basic formatting, for instance:
_text_ or *text* to italicise**text** for bold text# at the start of a new line for a title (and ## for second-level title, ## for third-level title etc)* at the start of a new line for bullet pointstext to display text as code (as above)The actual appearance of the font can be set by using specific templates (specified in the YAML metadata; see example tabs).
You can also include minimal R code within backwards ticks, for within-text values. See example below.
3. Code chunks: This is where the R code goes, for the actual data management and visualisation. To note: These ‘chunks’ will appear to have a slightly different background colour from the narrative part of the document.
Each chunk always starts with three backticks and chunk information within squiggly brackets, and ends with three more backticks.
Some notes about the content of the squiggly brackets:
There are also two arrows at the top right of each chunk, which are useful to run code within a chunk, or all code in prior chunks.
General notes
Everything used by this markdown must be referenced within the Rmd file. For instance, you need to load any required packages or data.
A single or test run from within R Markdown
To render a single document, for instance if you are testing it or if you only need to produce one rendered document at a time, you can do it from within the open R Markdown file. Click the button at the top of the document.
The ‘R Markdown’ tab will start processing to show you the overall progress, and a complete document will automatically open when complete. This document will also be saved in the same folder as your markdown, and with the same file name aside from the file extension. This is obviously not ideal for version control, as you will then rename the file yourself.
A single run from an separate script
To run the markdown so that a date-stamped file is produced, you can create a separate script and call the Rmd file from within it. You can also specify the folder and file name, and include a dynamic date and time, so that file will be date stamped on production.
rmarkdown::render(("rmd_reports/create_RED_report.Rmd"),
output_file = paste0("outputs/Report_", Sys.Date, ".docx")) # Use 'paste0' to combine text and code for a dynamic file name
Routine runs into newly created date-stamped sub folders
Add a couple lines of code to define the date you are running the report (e.g. using Sys.Date as in the example above) and create new sub folders. If you want the date to reflect a specific date rather than the current date, you can also enter it as an object.
# Set the date of report
refdate <- as.Date("2020-12-21")
# Create the folders
outputfolder <- paste0("outputs/", refdate) # This is the new folder name
dir.create(outputfolder) # Creates the folder (in this case assumed 'outputs' already exists)
#Run the loop
rmarkdown::render(("rmd_reports/create_report.Rmd"),
output_file = paste0(outputfolder, "/Report_", refdate, ".docx")) #Dyanmic folder name now included
You may want some dynamic information to be included in the markdown itself. This is addressed in the next section.
Parameterised reports are the next step so that the content of the R Markdown itself can also be dynamic. For example, the title can change according to the subgeography you are running, and the data can filter to that subgeography of interest.
Let’s say you want to run the markdown to produce a report with surveillance data for Area1 and Area2. You will:
filter(area == params$areanumber) rather than filter(area=="Area1").For instance (simplified version which does not include setup code such as library/data loading):
You can change the content by editing the YAML as needed, or set up a loop in a separate script to iterate through the areas. As with the previous section, you can set up the folders as well.
As you can see below, you set up a list which includes all areas of interest (arealist), and when rendering the markdown you specify that the parameterized areanumber for a specific iteration is the Nth value of the arealist. For instance, for the first iteration, areanumber will equate to “Area1”. The code below also specifies that the Nth area name will be included in the output file name.
Note that this will work even if an area or date are specified within the YAML itself - that YAML information will get overwritten by the loop.
# Set the date of report
refdate <- as.Date("2020-12-21")
# Set the list (note that this can also be an imported list)
arealist <- c("Area1", "Area2", "Area3", "Area4", "Area5")
# Create the folders
outputfolder <- paste0("outputs/", refdate) # This is the new folder name
dir.create(outputfolder) # Creates the folder (in this case assumed 'outputs' already exists)
#Run the loop
for(i in 1:length(arealist)) { # This will loop through from the first value to the last value in 'arealist'
rmarkdown::render(here("rmd_reports/create_report.Rmd"),
params = list(areanumber = arealist[1], #Assigns the nth value of arealist to the current areanumber
refdate = refdate),
output_file = paste0(outputfolder, "/Report_", arealist[1], refdate, ".docx"))
}
Further information can be found via:
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
Keep the title of this section as “Preparation”.
Data preparation steps such as:
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
for (row_origin in 1:nrow(ct_metrics)){
# print progress
if(row_origin %% 100==0){
print(row_origin)
}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Tidymodels
Liza Coyer TODO this? logitudinal data
R(t) estimations Doubling times Projections
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
rprofiles
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
https://www.tidyverse.org/blog/2018/07/ggplot2-3-0-0/
Embed ggplot cheatsheet
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
highlighting one line among many etc gghighlight
Cowplot Complicated method (% 100 * …)
ggrepel
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
Data visualisation is increasingly required to be interrogable by the audience. Consequently creating interactive plots are becoming common. There are several ways to include these but the two most important are {plotly} and {shiny}.
{Shiny} is covered in another part of this handbook, so we will only cover {plotly} here. #TODO - link to shiny page
Making plots interactive can sound more difficult than it turns out to be, thanks to some fantastic tools.
In this section, you’ll learn to easily make a plot interactive with {the wonders {ggplot2} and {plotly}
In the example you saw a very basic epicurve that had been transformed to bbe interactive using the fantastic {ggplot2} - {plotly} integrations. So to start, make a basic chart of your own:
Loading data
Manipulate and add columns (best taught in the epicurves section)
linelist <- linelist %>%
dplyr::mutate(
## If the outcome column is NA, change to "Unknown"
outcome = dplyr::if_else(condition = is.na(outcome),
true = "Unknown",
false = outcome),
## If the date of infection is NA, use the date of onset instead
date_earliest = dplyr::if_else(condition = is.na(date_infection),
true = date_onset,
false = date_infection),
## Summarise earliest date to earliest week
week_earliest = lubridate::floor_date(x = date_earliest,
unit = "week",
week_start = 1)
)Count for plotting
Make into a plot
p <- linelist %>%
ggplot()+
geom_col(aes(week_earliest, n, fill = outcome))+
xlab("Week of infection/onset") + ylab("Cases per week")+
theme_minimal()Make interactive
Voila!
When exporting in an Rmarkdown generated HTML (like this book!) you want to make the plot as small as possible (with no negative side effects in most cases). For this, just add add this line:
Some of the buttons on a standard plotly (as shown on the preparation tab) are superfluous and can be distracting, so it’s best to remove them. You can do this simply by piping the output into plotly::config
## these buttons are superfluous/distracting
plotly_buttons_remove <- list('zoom2d','pan2d','lasso2d', 'select2d','zoomIn2d',
'zoomOut2d','autoScale2d','hoverClosestCartesian',
'toggleSpikelines','hoverCompareCartesian')
p <- p %>%
plotly::config(displaylogo = FALSE, modeBarButtonsToRemove = plotly_buttons_remove)Earlier you saw #TODO link to heatmaps how to make heatmaps, and they are just as easy to make interactive.
You can even make interactive maps! However, they’re slightly trickier. Although {plotly} works well with ggplot2::geom_sf in RStudio, when you try to include it’s outputs in Rmarkdown HTML files (like this book), it doesn’t work well.
So instead you can use {plotly}’s own mapping tools which can be tricky but are easy when you know how. Read on…
We’re going to use Covid-19 incidence across African countries for this example. The data used can be found on the World Health Organisation website.
You’ll also need a new type of file, a GeoJSON, which is sort of similar to a shp file for those familiar with GIS. For this book, we used one from here.
GeoJSON files are stored in R as complex lists and you’ll need to maipulate them a little.
## You need two new packages: {rjson} and {purrr}
pacman::p_load(plotly, rjson, purrr)
## This is a simplified version of the WHO data
df <- rio::import(here::here("data", "covid_incidence.csv"))
## Load your geojson file
geoJSON <- rjson::fromJSON(file=here::here("data", "africa_countries.geo.json"))
## Here are some of the properties for each element of the object
head(geoJSON$features[[1]]$properties)
## $scalerank
## [1] 1
##
## $featurecla
## [1] "Admin-0 country"
##
## $labelrank
## [1] 6
##
## $sovereignt
## [1] "Burundi"
##
## $sov_a3
## [1] "BDI"
##
## $adm0_dif
## [1] 0This is the tricky part. For {plotly} to match your incidence data to GeoJSON, the countries in the geoJSON need an id in a specific place in the list of lists. For this we need to build a basic function:
## The property column we need to choose here is "sovereignt" as it is the names for each country
give_id <- function(x){
x$id <- x$properties$sovereignt ## Take sovereignt from properties and set it as the id
return(x)
}
## Use {purrr} to apply this function to every element of the features list of the geoJSON object
geoJSON$features <- purrr::map(.x = geoJSON$features, give_id)plotly::plot_ly() %>%
plotly::add_trace( #The main plot mapping functionn
type="choropleth",
geojson=geoJSON,
locations=df$Name, #The column with the names (must match id)
z=df$Cumulative_incidence, #The column with the incidence values
zmin=0,
zmax=57008,
colorscale="Viridis",
marker=list(line=list(width=0))
) %>%
plotly::colorbar(title = "Cases per million") %>%
plotly::layout(title = "Covid-19 cumulative incidence",
geo = list(scope = 'africa')) %>%
plotly::config(displaylogo = FALSE, modeBarButtonsToRemove = plotly_buttons_remove)Plotly is not just for R, but also works well with Python (and really any data science language as it’s built in JavaScript). You can read more about it on the plotly website
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
Using R on network or “company” shared drives can be extremely frustrating. This page contains approaches, common errors, and suggestions on troubleshooting, including for the particularly delicate situations involving Rmarkdown.
Using R on Network Drives: Overarching principles
Using R on Network Drives: Overarching principles
Useful commands
# Find libraries
.libPaths() # Your library paths, listed in order that R installs/searches.
# Note: all libraries will be listed, but to install to some (e.g. C:) you
# may need to be running RStudio as an administrator (it won't appear in the
# install packages library drop-down menu)
# Switch order of libraries
# this can effect the priority of R finding a package. E.g. you may want your C: library to be listed first
myPaths <- .libPaths() # get the paths
myPaths <- c(myPaths[2], myPaths[1]) # switch them
.libPaths(myPaths) # reassign them
# Find Pandoc
Sys.getenv("RSTUDIO_PANDOC") # Find where RStudio thinks your Pandoc installation is
# Find a package
# gives first location of package (note order of your libraries)
find.package("rmarkdown", lib.loc = NULL, quiet = FALSE, verbose = getOption("verbose")) “Failed to compile…tex in rmarkdown”
check/install tinytex, to C: location
# check/install tinytex, to C: location
tinytex::install_tinytex()
tinytex:::is_tinytex() # should return TRUE (note three colons)Internet routines cannot be loaded
For example, “Error in tools::startDynamicHelp() : internet routines cannot be loaded”
C: library does not appear as an option when I try to install packages manually
Pandoc 1 error
If you are getting pandoc error 1 when knitting Rmarkdowns on network drives:
myPaths <- .libPaths() # get the library paths
myPaths <- c(myPaths[2], myPaths[1]) # switch them
.libPaths(myPaths) # reassign themPandoc Error 83 (can’t find file…rmarkdown…lua…)
This means that it was unable to find this file.
Possibilities:
R is not able to find the ‘rmarkdown’ package file, so check which library the rmarkdown package lives. If it is in a library that in inaccessible (e.g. starts with "\") consider manually moving it to C: or other named drive library.
But be aware that the rmarkdown package has to be able to reach tinytex, so rmarkdown package can’t live on a network drive.
Pandoc Error 61 For example: “Error: pandoc document conversion failed with error 61”
“Could not fetch…”
LaTex error (see below)
“! Package pdftex.def Error: File `cict_qm2_2020-06-29_files/figure-latex/unnamed-chunk-5-1.png’ not found: using draft setting.”
“Error: LaTeX failed to compile file_name.tex.”
See https://yihui.org/tinytex/r/#debugging for debugging tips. See file_name.log for more info.
Pandoc Error 127 This could be a RAM (space) issue. Re-start your R session and try again.
Mapping network drives
How does one open a file “through a mapped network drive”?
ISSUES WITH HAVING A SHARED LIBRARY LOCATION ON NETWORK DRIVE
Error in install.packages()
Try removing… /../…/00LOCK (directory)
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
Saving files, deleting files, creating folders, interacting with files in a folder, etc Overwriting files in Excel
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
The Page title should be succinct. Consider adding a tag with no spaces into the curly brackets, such as below. This can be used for internal links within the handbook. {#title_tag .tabset .tabset-fade}
Keep the title of this section as “Preparation”.
Data preparation steps such as:
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.
Troubleshooting common errors and warnings
Keep the title of this section as “Overview”.
This tab should include:
Keep the title of this section as “Preparation”.
Data preparation steps such as:
Can be used to separate major steps of data preparation. Re-name as needed
#Tried to add a value ("Missing") to a factor (with replace_na operating on a factor)
Problem with `mutate()` input `age_cat`.
i invalid factor level, NA generated
i Input `age_cat` is `replace_na(age_cat, "Missing")`.invalid factor level, NA generated
# ran recode without re-stating the x variable in mutate(x = recode(x, OLD = NEW)
Error: Problem with `mutate()` input `hospital`.
x argument ".x" is missing, with no default
i Input `hospital` is `recode(...)`.
Error: Insufficient values in manual scale. 3 needed but only 2 provided. ggplot() scale_fill_manual() values = c(“orange”, “purple”) … insufficient for number of factor levels … consider whether NA is now a factor level…
Error: unexpected symbol in:
" geom_histogram(stat = "identity")+
tidyquant::geom_ma(n=7, size = 2, color = "red" lty"
If you see “unexpected symbol” check for missing commas
Wrong slashes If you see an error like this when you try to export or import:
No such file or directory:
Make sure you have used / within the filepath, not \.
Can be used to separate major steps of data preparation. Re-name as needed.
This tab can be renamed. This tab should demonstrate execution of the task using recommended package/approach. For example, using a package customized for this task where the execution is simple and fast but perhaps less customizable. For example using incidence package to create an epicurve.
Sub-tabs if necessary. Re-name as needed.
This tab can be re-named. This tab should demonstrate execution of the task a more standard/core package (e.g. ggplot2, or base R) that allows for more flexibility in the output or more package stability. For example, showing how to create an epicurve using ggplot2.
Sub-tabs if necessary. Re-name as needed.
This tab should stay with the name “Resources”. Links to other online tutorials or resources.